#python
#python
Вопрос:
Новичок в Python, не уверен, правильный ли это метод, но у меня возникают проблемы при попытке запустить следующее:
from Meal_Options import usual_meals, healthy_meals, hot_meals, other_meals, lunch_meals, fast_food_meals
print("Hello! Lets decide what you want for lunch! Please answer the first question.")
lunch_tea = input("Is it lunch, or tea? ").lower()
if lunch_tea == "lunch":
print(*lunch_meals, sep="n")
elif lunch_tea == "tea":
healthy = input("Healthy or not healthy? ").lower()
if healthy == "healthy":
food_type = input("Do you want spicy or not spicy? ").lower
if food_type == "not spicy":
print(*healthy_meals, sep="n")
print(*usual_meals, sep="n")
print(*other_meals, sep="n")
elif food_type == "spicy":
print(*hot_meals, sep="n")
elif healthy == "not healthy" or "unhealthy":
pizza_chinese_indian = input("Do you want pizza, chinese or indian? ").lower
if pizza_chinese_indian == "pizza":
print(*fast_food_meals)
elif pizza_chinese_indian == "chinese":
print(*fast_food_meals)
elif pizza_chinese_indian == "indian":
print("Ok, Korma it is")
elif lunch_tea != "tea" or "lunch":
print("n" "Invalid input, please enter 'Tea' or 'Lunch'")
finished = input("Are you happy with your selection? ").lower
Вывод:
Hello! Lets decide what you want for lunch! Please answer the first question.
Is it lunch, or tea? tea
Healthy or not healthy? not healthy
Do you want pizza, chinese or indian? chinese
Are you happy with your selection? no
Почему код ничего не печатает при ответе «пицца«, «китайская» или «индийская» и просто переходит к «Вы довольны своим выбором?»
Комментарии:
1. попробуйте еще раз… на этот раз ответ
unhealthy
Ответ №1:
Ваш второй аргумент or "lunch"
всегда будет оцениваться True
как, поскольку это строка ненулевой длины.
Должно быть
elif lunch_tea != "tea" or lunch_tea != "lunch":
или, еще лучше:
elif lunch_tea not in ("tea", "lunch"):
Ответ №2:
Я считаю, что основная проблема здесь в том, что вы забываете добавить метод ()
after str.lower
.
from Meal_Options import usual_meals, healthy_meals, hot_meals, other_meals, lunch_meals, fast_food_meals
...
if lunch_tea == "lunch":
print(*lunch_meals, sep="n")
elif lunch_tea == "tea":
...
if healthy == "healthy":
food_type = input("Do you want spicy or not spicy? ").lower()
...
elif healthy == "not healthy" or "unhealthy":
pizza_chinese_indian = input("Do you want pizza, chinese or indian? ").lower()
...
...
finished = input("Are you happy with your selection? ").lower()
Я скопировал только соответствующий код, чтобы точно указать, где находятся проблемы.
Ответ №3:
вы, вероятно, хотите in
или хотите иметь что-то вроде
if healthy == "not healthy" or healthy == "unhealthy"
Проблема в том, что вы формируете структуру, подобную
if healthy == "not healthy" or "unhealthy"
if (var == test) or (True):