简体   繁体   English

在python函数中使用多个参数

[英]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). 定义一个名为food的函数,该函数接收两个参数:一个整数值,该值表示一天中的时间(以小时为单位),范围为0到24;一个布尔值,表示一个人是否喜欢糖果(真)(错)。 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). 如果早于6,则消息应显示“无食物”(无论是否喜欢甜食的人)。

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). 如果包含的极限值在6到10之间,则该消息应显示“早餐”,并且如果该人喜欢糖果,则在早餐一词之后,还应添加逗号和“果酱”一词,否则(如果该人不喜欢糖果,现在是早餐时间),早餐一词后应有一个逗号和“咖啡”一词(逗号后不能有空格)。 Then, if the time is between 11 and 15 (extremes included), the message should say "lunch", and if the person likes sweets. 然后,如果时间在11到15之间(包括极端时间),则消息应显示“午餐”,并且该人喜欢糖果。 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". 同样,如果在15点之后或22点之前,该消息将显示“晚餐”,并且与午餐类似,如果该人喜欢吃糖果,则会出现一个逗号,然后是“甜点”。 If it is 22 or later the returned messages should be again "no food". 如果它是22或更高版本,则返回的消息应再次为“ 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; (1)在一个函数中,您不能(有意义地)有两个return语句; 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" 按照书面规定,您将返回“沙漠”,并且永远没有机会获得“ 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): 也许这是关于返回的教程(python文档不是学习return语句的好手,它们有点技术性):

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

(2) You're comparing strings to booleans. (2)您正在将字符串与布尔值进行比较。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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