python - 关于 if-elif-if-if 链的初学者问题
问题描述
最近开始学习 Python(2 周前)并且一直很享受阅读 Python 速成课程这本书。我为书中的练习编写了一些代码。该代码按我的预期工作,但我不太明白为什么。
练习:为电影院编写一个程序,要求用户输入他们的年龄,然后使用 while 循环告诉他们门票的价格。
这是我第一次写的代码:
print("Welcome to Cathrine's Cinema!\n")
no_tickets = 0
total_cost = 0
while True:
if no_tickets == 0:
ticket = input("Would you like to buy a ticket? (Yes/No) ")
if ticket.lower() == 'yes':
no_tickets += 1
age = input("Great! How old is the person that this ticket is "
"for? ")
age = int(age)
if age < 3:
print("The ticket is free, enjoy!")
continue
elif age <= 12:
print("That'll be £10 please.")
total_cost += 10
continue
elif age > 12:
print("That'll be £15 please.")
total_cost += 15
continue
elif ticket.lower() == 'no':
print("Ok, thanks for coming by!")
break
else:
print("Please answer 'Yes' or 'No'")
continue
if no_tickets > 0:
ticket = input("Would you like to buy another ticket? (Yes/No) ")
if ticket.lower() == 'yes':
no_tickets += 1
age = input("Great! How old is the person that this ticket is "
"for? ")
age = int(age)
if age < 3:
print("The ticket is free, enjoy!")
continue
elif age <= 12:
print("That'll be £10 please.")
total_cost += 10
continue
elif age > 12:
print("That'll be £15 please.")
total_cost += 15
continue
elif ticket.lower() == 'no':
print(f"Cool, you have purchased {no_tickets} tickets for a total"
f" cost of £{total_cost}.")
break
else:
print("Please answer 'Yes' or 'No'")
continue
我认为只有两个大的几乎相同的 if 语句是非常尴尬的,所以最初的消息(你想买一个/另一个......)和再见消息是正确的,所以我又写了一点不同的东西。
print("Welcome to Cathrine's Cinema!\n")
no_tickets = 0
total_cost = 0
while True:
if no_tickets == 0:
message = "Would you like to buy a ticket? (Yes/No) "
bye_message = "Ok, thanks for coming by."
elif no_tickets > 0:
message = "Would you like to buy another ticket? (Yes/No) "
bye_message = (f"Cool, you have purchased {no_tickets} tickets for a "
f"total cost of £{total_cost}.")
ticket = input(message)
if ticket.lower() == 'yes':
no_tickets += 1
age = input("Great! How old is the person that this ticket is "
"for? ")
age = int(age)
if age < 3:
print("The ticket is free, enjoy!")
continue
elif age <= 12:
print("That'll be £10 please.")
total_cost += 10
continue
elif age > 12:
print("That'll be £15 please.")
total_cost += 15
continue
elif ticket.lower() == 'no':
print(bye_message)
break
else:
print("Please answer 'Yes' or 'No'")
continue
现在这似乎与之前的程序完全相同,但我对 if-elif 链感到困惑。我认为 python 在 if-elif 链中只执行一个块。因此,如果客户订购了 1 张票,则 no_tickets > 0,因此我们进入第二个 elif 语句。那我们为什么不回到 while 循环的开始并无限循环呢?为什么我们要继续下面的其他 if 语句(测试 ticket.lower() == 'yes' 或 'no')?
感谢您阅读所有这些!抱歉,如果这似乎是一个毫无意义的问题,因为我的代码按预期工作,我只是想正确理解正在发生的一切。
解决方案
这与缩进有关。像java这样的语言将 if/else 语句括在括号 {} 中,但 python 依赖于缩进。请注意, 与testing if ticket.lower() == 'yes' or 'no'
具有相同的缩进if no_tickets == 0:
,if no_tickets > 0:
因此它被认为在第一个 if/else 块之外。
这可能会有所帮助:https ://www.w3schools.in/python-tutorial/concept-of-indentation-in-python/
推荐阅读
- reactjs - Redux mapStateToProps 到类反应组件
- java - 在加入操作中使用实体 ID 时调用 JPA PreUpdate
- azure-active-directory - 有没有办法在不使用 AAD 作为身份验证的情况下为客户嵌入 Power Bi 报告?
- groovy - UnetStack:如何在两个调制解调器之间自动传输数据
- regression - 我应该如何格式化 logit 回归来测试公司的动机?(右)
- regex - VBA 正则表达式 REGEX : N.NN% 或 N.NNNN
- powershell - 使用 powershell 删除 Skype for business
- javascript - 将数据发送到 php 以从数据库中重新运行数据
- spring-kafka - 几个小时后,Kafka 听众停止阅读主题
- python - 为什么 pygame 默认不运行 pygame.init() ?