它应该打印:
no food
breakfast,marmalade
breakfast,coffee
lunch,dessert
dinner
no food
no food
dinner,dessert
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 ','.join((food_type, dessert))
[基本上现在,我在return''之间有一个逗号,因此它将显示breakfast, marmalade
,但是当涉及到no food
时,它将在末尾添加一个逗号,因此看起来像no food,
它看起来像:
no food
breakfast,marmalade
breakfast,coffee
lunch,dessert
dinner
no food
no food
dinner,dessert
我不确定要输出的内容是否100%,但我想即使dessert
为food_type
为"no food"
还是boolean
为True
,也不要使用逗号C0]。我至少可以想到两种不同的方法。
第一种方法是替换
food_type = "no food"
with
return "no food"
另一种方法是替换
return ','.join((food_type, dessert))
with
output = food_type
if output != "no food":
output = ','.join((food_type, dessert))
return output
有些人会喜欢第二个,因为只有一个return
。