Reputation: 33
We had a below task, we that tried to write code in what ever best possible way we can. But we are not able to pass the test as seems there is some issue in the code. hence need help to correct the code. request if some one can help us here it will be great for us.
from flask import Flask
## Define a flask application name 'app' below
app = Flask(__name__)
## Define below a view function 'hello', which displays the message
## "Hello World!!! I've run my first Flask application."
## The view function 'hello' should be mapped to URL '/' .
@app.route("/")
def hello():
return "Hello World!!! I've run my first Flask application."
## Define below a view function 'hello_user', which takes 'username' as an argument
## and returns the html string containing a 'h2' header "Hello <username>"
## After displaying the hello message, the html string must also display one quote,
## randomly chosen from the provided list `quotes`
# Before displaying the quote, the html string must contain the 'h3' header 'Quote of the Day for You'
## The view function 'hello_user' should be mapped to URL '/hello/<username>/' .
## Use the below list 'quotes' in 'hello_user' function
## quotes = [
## "Only two things are infinite, the universe and human stupidity, and I am not sure about the former.",
## "Give me six hours to chop down a tree and I will spend the first four sharpening the axe.",
## "Tell me and I forget. Teach me and I remember. Involve me and I learn.",
## "Listen to many, speak to a few.",
## "Only when the tide goes out do you discover who has been swimming naked."
## ]
@app.route("/hello/<username>/")
def hello_user(username):
return "Hello " + username + "Quote of the Day for You"
## Define below a view function 'display_quotes', which returns an html string
## that displays all the quotes present in 'quotes' list in a unordered list.
## Before displaying 'quotes' as an unordered list, the html string must also include a 'h1' header "Famous Quotes".
## The view function 'display_quotes' should be mapped to URL '/quotes/' .
## Use the below list 'quotes' in 'display_quotes' function
## quotes = [
## "Only two things are infinite, the universe and human stupidity, and I am not sure about the former.",
## "Give me six hours to chop down a tree and I will spend the first four sharpening the axe.",
## "Tell me and I forget. Teach me and I remember. Involve me and I learn.",
## "Listen to many, speak to a few.",
## "Only when the tide goes out do you discover who has been swimming naked."
## ]
@app.route("/quotes/")
def display_quotes():
return render_template( 'test.html',name=display_quotes)
quotes = [ "Only two things are infinite, the universe and human stupidity, and I am not sure about the former.",
"Give me six hours to chop down a tree and I will spend the first four sharpening the axe.",
"Tell me and I forget. Teach me and I remember. Involve me and I learn.",
"Listen to many, speak to a few.",
"Only when the tide goes out do you discover who has been swimming naked."]
randomNumber = randint(0,len(quotes)-1)
quote = quotes[randomNumber]
## Write the required code below which runs flask applictaion 'app' defined above
## on host 0.0.0.0 and port 8000
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)
Please let us know where in mistake in it and help us in correcting code and pass required test.
Upvotes: 2
Views: 3670
Reputation: 89
from flask import Flask
import random
Define a flask application name 'app' below
app = Flask(__name__)
Define below a view function 'hello', which displays the message "Hello World!!! I've run my first Flask application." The view function 'hello' should be mapped to URL '/' .
@app.route("/")
def hello():
return "Hello World!!! I've run my first Flask application."
Define below a view function 'hello_user', which takes 'username' as an argument
and returns the html string containing a 'h2' header "Hello "
After displaying the hello message, the html string must also display one quote,
randomly chosen from the provided list quotes
Before displaying the quote, the html string must contain the 'h3' header 'Quote of the Day for You'
The view function 'hello_user' should be mapped to URL '/hello//' .
Use the below list 'quotes' in 'hello_user' function
quotes = [
"Only two things are infinite, the universe and human stupidity, and I am not sure about the former.",
"Give me six hours to chop down a tree and I will spend the first four sharpening the axe.",
"Tell me and I forget. Teach me and I remember. Involve me and I learn.",
"Listen to many, speak to a few.",
"Only when the tide goes out do you discover who has been swimming naked."
]
@app.route("/hello/<username>/")
def hello_user(username):
quotes = [
"Only two things are infinite, the universe and human stupidity, and I am not sure about the former.",
"Give me six hours to chop down a tree and I will spend the first four sharpening the axe.",
"Tell me and I forget. Teach me and I remember. Involve me and I learn.",
"Listen to many, speak to a few.",
"Only when the tide goes out do you discover who has been swimming naked."
]
return "<h2>Hello " + username + "</h2><h3>Quote of the Day for You</h3>" + random.choice(quotes)
Define below a view function 'display_quotes', which returns an html string that displays all the quotes present in 'quotes' list in a unordered list. Before displaying 'quotes' as an unordered list, the html string must also include a 'h1' header "Famous Quotes". The view function 'display_quotes' should be mapped to URL '/quotes/' . Use the below list 'quotes' in 'display_quotes' function quotes = [ "Only two things are infinite, the universe and human stupidity, and I am not sure about the former.", "Give me six hours to chop down a tree and I will spend the first four sharpening the axe.", "Tell me and I forget. Teach me and I remember. Involve me and I learn.", "Listen to many, speak to a few.", "Only when the tide goes out do you discover who has been swimming naked." ]
@app.route("/quotes/")
def display_quotes():
quotes = [
"Only two things are infinite, the universe and human stupidity, and I am not sure about the former.",
"Give me six hours to chop down a tree and I will spend the first four sharpening the axe.",
"Tell me and I forget. Teach me and I remember. Involve me and I learn.",
"Listen to many, speak to a few.",
"Only when the tide goes out do you discover who has been swimming naked."
]
return "<h1>Famous Quotes</h1><ul><li>"+ quotes[0] +"</li><li>"+ quotes[1] +"</li><li>"+ quotes[2] +"</li><li>"+ quotes[3] +"</li><li>"+ quotes[4] +"</li></ul>"
Write the required code below which runs flask applictaion 'app' defined above on host 0.0.0.0 and port 8000
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)
Upvotes: 5
Reputation: 2435
temp <- c(103,100,98)
my.function <- function(x){ifelse(x>100,"Bad","Good")}
# this creates a vector
my.function(temp)
[1] "Bad" "Good" "Good"
# this prints on the console, element after element
for(i in 1:length(temp)){
print(my.function(temp[i]))
}
[1] "Bad"
[1] "Good"
[1] "Good"
# a third alternative using your code
for(i in 1:length(temp)){
if(temp[i] > 100)
{
print("hot")
}
else
{
print("good")
}
}
[1] "hot"
[1] "good"
[1] "good"
Upvotes: 0