首页 > 解决方案 > 关于 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')?

感谢您阅读所有这些!抱歉,如果这似乎是一个毫无意义的问题,因为我的代码按预期工作,我只是想正确理解正在发生的一切。

标签: pythonlist

解决方案


这与缩进有关。像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/


推荐阅读