为什么循环后会忽略else语句?

时间:2018-09-21 00:20:59

标签: python-3.x

我正在尝试使else语句在循环后起作用,但是在第二个循环中,它将被忽略,并且当输入[Y / N]以外的值时,tickets_left值将更新

票务机

tickets_left = 100

ticket_price = 5

while tickets_left >= 0:

    print("There are {} tickets left".format(tickets_left))

    buy = input("How many tickets would you like to buy: ")

    buy = int(buy)

    price = buy * ticket_price

    confirm = input("Are you sure you would like to buy {} tickets for a total of ${} [Y/N]: ".format(buy,price))

    tickets_left = tickets_left - buy



if confirm.upper() == "Y":
    print("Sold for ${}".format(price))
if confirm.upper() == "N":
    print("Ok")
else:
    print("Please enter either [Y/N] to proceed")
    buy = input("How many tickets would you like to buy: ")

0 个答案:

没有答案