while循環的運用


# 7-1 汽車租賃 :編寫一個程序,詢問用戶要租賃什么樣的汽車,並打印一條消息,如“Let me see if I can find you a Subaru”。

car = input('Hello, What is car would you want to rent: ')
print('Let me see if I can find you a ' + car)


# 7-2 餐館訂位 :編寫一個程序,詢問用戶有多少人用餐。如果超過8人,就打印一條消息,指出沒有空桌;否則指出有空桌。

import re
people = input('How many people will come for dinner:')
'''利用正則表達式提取people里的數字'''
number = re.sub('\D','',people)
print(number)

if int(number) <= 8:
print('Lucky! There have only a table for you.')
else:
print("It's very sorry, we don't have free table.")

#7-3 10的整數倍 :讓用戶輸入一個數字,並指出這個數字是否是10的整數倍。

number = input('請輸入一個數字:')
if int(number) % 10 == 0 :

print('%s是10的整數倍。'%number)
else:
print('%s不是10的整數倍。'%number)
 
# 7-4 比薩配料 :編寫一個循環,提示用戶輸入一系列的比薩配料,並在用戶輸入'quit' 時結束循環。每當用戶輸入一種配料后,都打印一條消息,說我們會在比薩
中添加這種配料

pizzas_add = '請輸入你要添加的配料:'
pizzas_add += '\n(當你想退出程序時,請輸入quit)'
while True:
add = input(pizzas_add)
if add == 'quit':
break

else:
print('非常抱歉,%s暫時沒有,下次我們會加入您的這種配料,非常感謝您光顧我們的店。'% add)



 
        
# 7-5 電影票 :有家電影院根據觀眾的年齡收取不同的票價:不到3歲的觀眾免費;3~12歲的觀眾為10美元;超過12歲的觀眾為15美元。請編寫一個循環,在其中詢問用
# 戶的年齡,並指出其票價。


import re
ages = '請輸入你的年齡(當你想退出程序時,請輸入quit):'

while True:
age = input(ages)
#age_info = re.sub('\D','','age')
if age == 'quit':
break
elif int(age) < 3:
print('不到3歲的觀眾免費。')
elif int(age) <= 12:
print('3-12歲的觀眾為10美元。')
else:
print('超過12歲的觀眾為15美元。')
 
 
        
# 7-6 三個出口 :以另一種方式完成練習7-4或練習7-5,在程序中采取如下所有做法。
# 在while 循環中使用條件測試來結束循環。
# 使用變量active 來控制循環結束的時機。
# 使用break 語句在用戶輸入'quit' 時退出循環。



 
        
ages = '請輸入你的年齡(當你想退出程序時,請輸入quit):'
#設置一個標志,指出調查是否繼續
active = True
while active:
age = input(ages)
#age_info = re.sub('\D','','age')
if age == 'quit':
active = False
elif int(age) < 3:
print('不到3歲的觀眾免費。')
elif int(age) <= 12:
print('3-12歲的觀眾為10美元。')
else:
print('超過12歲的觀眾為15美元。')
 
# 7-8 熟食店 :創建一個名為sandwich_orders 的列表,在其中包含各種三明治的名字;再創建一個名為finished_sandwiches 的空列表。遍歷列
# 表sandwich_orders ,對於其中的每種三明治,都打印一條消息,如I made your tuna sandwich ,並將其移到列表finished_sandwiches 。所有三明
# 治都制作好后,打印一條消息,將這些三明治列出來。


 
        
sandwich_orders = ['tata','apple','berry']
finished_sandwiches = []

for sandwich_order in sandwich_orders:
print('I made your %s sandwich.'% sandwich_order)
finished_sandwiches.append(sandwich_order)

print(','.join(finished_sandwiches))

# join()函數:連接列表里面的所有元素
print('I finished your ' + ','.join(finished_sandwiches) + 'sandwiches')


# 7-9 五香煙熏牛肉(pastrami)賣完了 :使用為完成練習7-8而創建的列表sandwich_orders ,並確保'pastrami' 在其中至少出現了三次。在程序開頭附近添加
# 這樣的代碼:打印一條消息,指出熟食店的五香煙熏牛肉賣完了;再使用一個while 循環將列表sandwich_orders 中的'pastrami' 都刪除。確認最終的列
# 表finished_sandwiches 中不包含'pastrami' 。


sandwich_orders = ['pastrami','tata','apple','pastrami','berry','bacon','pastrami']
finished_sandwiches = []
print('The pastrami sandwich is sell out.')

while 'pastrami' in sandwich_orders:

sandwich_orders.remove('pastrami')
for sandwich_order in sandwich_orders:
finished_sandwiches.append(sandwich_order)

print('We only have ' + ','.join(finished_sandwiches) + ' sandwiches.')


 
        
# 7-10 夢想的度假勝地 :編寫一個程序,調查用戶夢想的度假勝地。使用類似於“If you could visit one place in the world, where would you go?”的提示,並編寫一個打印調查
# 結果的代碼塊。


responses = {}

#設置一個標志,指出調查是否繼續。
polling_active = True

while polling_active:
#提示輸入被調查者的名字和答案
name = input('\nWhat is your name?')
response = input('If you could visit one place in the world, where would you go?')

#將答案存儲在字典里
responses[name] = response

#看看是否還有人要參與調查
repeat = input('Would you like to let another person respond?(yes/no)')
if repeat == 'no':
polling_active = False

#調查結束,顯示結果
print('\n--- Poll Results---')
for name,response in responses.items():
print(name + ',would like to go to ' + response + '.')




 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM