1. 程式人生 > 其它 >Python|while迴圈:電影院票價根據不同年齡收費

Python|while迴圈:電影院票價根據不同年齡收費

技術標籤:python

(1)使用break語句結束

while True:
    age = input("your age is:")
    if age == 'quit':
        break
    elif int(age) < 3:
        print("price is free")
    elif int(age) > 12:
        print("price is 15")
    else:
        print("price is 10"
)

輸入quit時結束迴圈
在這裡插入圖片描述
(2)使用條件測試結束

age = ''
while age != 'quit':
    age = input("your age is:")
    if int(age) < 3:
        print("price is free")
    elif int(age) > 12:
        print("price is 15")
    elif int(age) >= 3 and int(age) <= 12:
        print("price is 10"
)

(3)使用變數active控制結束時機

active = True
while active:
    age = input("your age is:")
    if age == 'quit':
        active = False
    elif int(age) < 3:
        print("price is free")
    elif int(age) > 12:
        print("price is 15")
    elif int(age) >= 3 and
int(age) <= 12: print("price is 10")