題目:
7-4 比薩配料 :編寫一個循環,提示用戶輸入一系列的比薩配料,並在用戶輸入'quit' 時結束循環。每當用戶輸入一種配料后,都打印一條消息,說我們會在比薩
中添加這種配料。
7-5 電影票 :有家電影院根據觀眾的年齡收取不同的票價:不到3歲的觀眾免費;3~12歲的觀眾為10美元;超過12歲的觀眾為15美元。請編寫一個循環,在其中詢問用
戶的年齡,並指出其票價。
7-6 三個出口 :以另一種方式完成練習7-4或練習7-5,在程序中采取如下所有做法。
在while 循環中使用條件測試來結束循環。
使用變量active 來控制循環結束的時機。
使用break 語句在用戶輸入'quit' 時退出循環。
7-7 無限循環 :編寫一個沒完沒了的循環,並運行它(要結束該循環,可按Ctrl+C,也可關閉顯示輸出的窗口)。
代碼:
#!usr/bin/python # _*_ coding:utf-8 _*_ #披薩配料 prompt = "請輸入要添加的配料(按quit退出點餐):" active = True while active: toppings = input(prompt) if toppings == "quit": active = False else: print("我們將會在披薩中添加你想要的%s"%toppings) #電影票 prompt = "請問你多大?" active = True while active: age = int(input(prompt)) if age < 3: print("小於三歲可以免票") elif age>3 and age<12: print("請付十美元") elif age >12: print("請付15美元") #三個出口 #披薩配料2 prompt_1 = "請輸入要添加的配料(按quit退出點餐):" active_1 = True while active_1: toppings = input(prompt_1) if toppings == "quit": active_1 = False else: print("我們將會在披薩中添加你想要的%s"%toppings) prompt_2 = "請輸入要添加的配料(按quit退出點餐):" active_2 = True while active_2: toppings = input(prompt_2) if toppings == "quit": break print("我們將會在披薩中添加你想要的%s"%toppings) #無限循環 i = 1 while i <10: print(i)