Определите функцию с именем food, которая получает два параметра - PullRequest
0 голосов
/ 26 октября 2019

Предполагается, что будет напечатано:

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

1 Ответ

2 голосов
/ 26 октября 2019

Я не уверен на 100% в том выводе, который вам нужен, но я думаю, что вы не хотите иметь dessert или запятую, если food_type будет "no food", даже если booleanTrue. Я могу придумать как минимум два разных способа сделать это.

Первый способ - заменить

    food_type = "no food"

на

    return "no food"

Другой способ - заменить

  return ','.join((food_type, dessert))

с

  output = food_type
  if output != "no food":
    output = ','.join((food_type, dessert))
  return output

Некоторые люди предпочли бы второе, так как есть только один return.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...