简体   繁体   中英

using multiple parameters in python function

Here is the question I am trying to solve:

Define a function named food which receives two parameters: an integer value representing the time of the day measured in hours from 0 to 24 and a Boolean value indicating whether a person likes sweets (True) or not (False). The function should return one single string with a message as follows.

If it is earlier than 6, the message should say "no food" (regardless of the person liking sweets or not).

If it is between 6 and 10 extremes included, the message should indicate "breakfast", and if the person likes sweets, additionally, after the word breakfast, there should be a comma and then the word "marmalade", otherwise (if the person does not like sweets and it is breakfast time), after the word breakfast there should be a comma and the word "coffee" (with no spaces after the comma). Then, if the time is between 11 and 15 (extremes included), the message should say "lunch", and if the person likes sweets. additionally, after the word "lunch" there will be a comma and then the word "dessert". Similarly, if it is after 15 or before 22 the message will indicate "dinner", and similarly to lunch, if the person likes sweets there will be a comma and then the word "dessert". If it is 22 or later the returned messages should be again "no food".

For example

food(4,False) should return "no food"
food(7,True) should return the message "breakfast,marmalade"
food(7,False) should return "breakfast,coffee"
food(12,True) should return "lunch,dessert"
food(20,False) should return "dinner"

As an example, the following code fragment:

print food(7,True)

should produce the output:

breakfast,marmalade

And here is what I have and I am stuck! Help please.

def food(input,boolean):
    time = int(input)
    food_type = ""
    if time >= 0 and time < 6 or time >= 22:
        food_type = "no food"
    if time >= 6 and time <= 10:
        food_type = "breakfast"
    if time >= 11 and time <= 15:
        food_type = "lunch"
    if time >= 16 and time < 22:
        food_type = "dinner"
    dessert = ""
    if boolean == "True" and food_type == "breakfast":
        dessert = "marmalade"
    if boolean == "False" and food_type == "breakfast":
        dessert = "coffee"
    if boolean == "True" and food_type == "lunch":
        dessert = "dessert"
    if boolean == "True" and food_type == "dinner":
        dessert = "dessert"

    return dessert
    return food_type
print food(7,True)

You have two problems:

(1) You can't (meaningfully) have two return statements in a function after one another; when a return evaluates, it ends the function. What you meant to write was

return (desert,food_type)

To get both of them at once. As written, you return "desert" and never have a chance to get at "food_type"

See this post for details:

Is there a way to do more work after a return statement?

And maybe this tutorial on returning (the python documentation isn't great for learning about return statements, they're kind of technical):

http://learnpythonthehardway.org/book/ex21.html

(2) You're comparing strings to booleans. What you meant was

if boolean == True: 

not

if boolean == "True":
def food(input,boolean):
    time = int(input)
    food_type = ""
    if time >= 0 and time < 6 or time >= 22:
        food_type = "no food"
    if time >= 6 and time <= 10:
        food_type = "breakfast"
    if time >= 11 and time <= 15:
        food_type = "lunch"
    if time >= 16 and time < 22:
        food_type = "dinner"
    dessert = ""
    if boolean == True and food_type == "breakfast":
        dessert = "marmalade"
    if boolean == False and food_type == "breakfast":
        dessert = "coffee"
    if boolean == True and food_type == "lunch":
        dessert = "dessert"
    if boolean == True and food_type == "dinner":
        dessert = "dessert"
    return (dessert, food_type)
     # return food_type
print food(7,True)

produce ('marmalade', 'breakfast') if you want to use one of them you should use it like this

raw = food(7, True)
print raw[0]
print raw[1]

it will produce

marmalade
breakfast

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM