嵌套的 if/else 语句出现问题。如何让它打印"else:"语句?



代码如下:

# Write a program that asks the user how many people
# are in their dinner group. If the answer is more than eight, print a message saying
# they’ll have to wait for a table. Otherwise, report that their table is ready.
people = input("How many people will you be having in your dinner group? ")
people = int(people)
if people > 8:
print(input("We'll have to put you on a short wait, is that okay?" ))
if 'yes':
print("Okay, we will call your table in the next 15 minutes.")
else:
print("Okay, we will see you another night, then. Thank you for stopping by.")
else:
print("Perfect! Right this way; follow me.")

我不确定我的第二个"如果"这个陈述是正确的,因为我想让它这样,如果有人说"是"还有句子里的其他东西,或者"是"在句子的后面,它会打印出("Okay,我们将在接下来的15分钟内调用您的表。")打印语句。

目前,如果我输入任何东西,(在回答第一个问题的数字大于8之后)即使是"no";它仍然会打印("好的,我们将在接下来的15分钟内调用您的表")语句。我想要同样的"是"。上面解释了发生的"不"。

我试着在if后面加上"是",但我觉得我错过了一些东西。'else'也一样。

您必须将输入的结果存储在一个变量中:

choice = input("We'll have to put you on a short wait, is that okay?" )
if choice == 'yes':
# Do something
else:
# Do something else

您没有保存响应的值以便能够在第二个if中检查它:

if people > 8:
response = input("We'll have to put you on a short wait, is that okay?")
if response == 'yes':
print("Okay, we will call your table in the next 15 minutes.")
else:
print("Okay, we will see you another night, then. Thank you for stopping by.")
else:
print("Perfect! Right this way; follow me.")

最新更新