《Python程式設計:從入門到實踐》第7章 使用者輸入和while迴圈
第7章 使用者輸入和while迴圈
7-1 汽車租賃
編寫一個程式,詢問使用者要租賃什麼樣的汽車,並列印一條訊息,如“Let me see if I can find you a Subaru”。
car = input("What kind of car do you want to rent? ")
print("Let me see if I can find you a " + car.title() + ".")
7-2 餐館定位
編寫一個程式,詢問使用者有多少人用餐。如果超過8人,就列印一條訊息,指出沒有空桌;否則指出有空桌。
dining = int(input("Hello, how many people do you have? " ))
if dining > 8:
print("I am very sorry, we don't have extra empty tables.")
else:
print("We have extra empty tables, please come with me.")
7-3 10的整數倍
讓使用者輸入一個數字,並指出這個數字是否是10的整數倍。
number = int(input("Please enter the number: "))
if number % 10 == 0:
print("The number you entered is an integer multiple of 10." )
else:
print("The number you entered is not an integer multiple of 10.")
7-4 比薩配料
編寫一個迴圈,提示使用者輸入一系列的比薩配料,並在使用者輸入’quit’ 時結束迴圈。每當使用者輸入一種配料後,都列印一條訊息,說我們會在比薩中新增這種配料。
prompt = "\nPlease enter the pizza ingredients you want to add. "
prompt += "\nIf you don't want to add ingredients, please type quit: "
while True:
pizza = input(prompt)
if pizza == 'quit':
break
else:
print("We will add " + pizza + " to the pizza.")
7-5 電影票
有家電影院根據觀眾的年齡收取不同的票價:不到3歲的觀眾免費;3~12歲的觀眾為10美元;超過12歲的觀眾為15美元。請編寫一個迴圈,在其中詢問使用者的年齡,並指出其票價。
ticket = int(input("Cinemas are based on age, please enter your age: "))
if ticket < 3:
print("According to your age, the fare is free.")
elif (ticket >= 3 && ticket <= 12):
print("According to your age, the fare is $10.")
elif ticket > 12:
print("According to your age, the fare is $15.")
7-6 三個出口
以另一種方式完成練習7-4或練習7-5,在程式中採取如下所有做法。
- 在while 迴圈中使用條件測試來結束迴圈。
- 使用變數active 來控制迴圈結束的時機。
- 使用break 語句在使用者輸入’quit’ 時退出迴圈。
prompt = "\nPlease enter the pizza ingredients you want to add. "
prompt += "\nIf you don't want to add ingredients, please type quit: "
active = True
while active:
pizza = input(prompt)
if pizza == 'quit':
active = False
else:
print("We will add " + pizza + " to the pizza.")
7-7 無限迴圈
編寫一個沒完沒了的迴圈,並執行它(要結束該迴圈,可按Ctrl +C,也可關閉顯示輸出的視窗)。
a = 1
while True:
print(a)
a += 1
7-8 熟食店
建立一個名為sandwich_orders 的列表,在其中包含各種三明治的名字;再建立一個名為finished_sandwiches 的空列表。遍歷列表sandwich_orders ,對於其中的每種三明治,都列印一條訊息,如I made your tuna sandwich ,並將其移到列表finished_sandwiches 。所有三明治都製作好後,列印一條訊息,將這些三明治列出來。
sandwich_orders = ['tuna', 'ham', 'bacon', 'chicken']
finished_sanwiches = []
for orders in sandwich_orders:
print("I made your " + orders.title() + " sandwich.")
finished_sanwiches.append(orders)
print("\nThe following sandwiches have been made: ")
for finished in finished_sanwiches:
print("\t" + finished.title() + " sandwich")
7-9 五香菸薰牛肉(pastrami)賣完了
使用為完成練習7-8而建立的列表sandwich_orders ,並確保’pastrami’ 在其中至少出現了三次。在程式開頭附近新增這樣的程式碼:列印一條訊息,指出熟食店的五香菸薰牛肉賣完了;再使用一個while 迴圈將列表sandwich_orders 中的’pastrami’ 都刪除。確認最終的列表finished_sandwiches 中不包含’pastrami’ 。
sandwich_orders = ['tuna', 'ham', 'bacon', 'chicken', "pastrami", "pastrami", "pastrami"]
finished_sanwiches = []
print("Guests, the smoked beef in the store ha been sold out.\n")
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
for orders in sandwich_orders:
print("I made your " + orders.title() + " sandwich.")
finished_sanwiches.append(orders)
print("\nThe following sandwiches have been made: ")
for finished in finished_sanwiches:
print("\t" + finished.title() + " sandwich")
7-10 夢想的度假勝地
編寫一個程式,調查使用者夢想的度假勝地。使用類似於“If you could visit one place in the world, where would you go?”的提示,並編寫一個列印調查結果的程式碼塊。
print("Welcome, this is a small program that investigates the user's dreams.")
print("We will draw a lottery based on the information left by the user and announce the results of the draw after 3 working days.")
name = input("Please enter your name: \n")
local = input("If you could visit one place in the world, where would you go?\n")
phone = input("Please leave your contact number: \n")
print("Here is the information you left behind: ")
print("Name: " + name + "\nLocal: " + local + "\nPhone: " + phone)
print("We have recorded the information you left and will announce the results of the draw after 3 working days.")
print("I wish you a happy life, bye bye")