1. 程式人生 > >Python程式設計:從入門到實踐(課後習題4)

Python程式設計:從入門到實踐(課後習題4)

7-1 汽車租賃 :編寫一個程式,詢問使用者要租賃什麼樣的汽車,並列印一條訊息,如“Let me see if I can find you a Subaru”。

#7-1
car = input("what car are you want ")
print("let me see if I can find you a "+car)

7-2 餐館訂位 :編寫

一個程式,詢問使用者有多少人用餐。如果超過8人,就列印一條訊息,指出沒有空桌;否則指出有空桌。

#7-2
num = input("How many people ")
num = int(num)
if num>8 :
    print('no table')
else :
    print('OK')

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

#7-3
num = input("Please input a number ")
num = int(num)
if num%10==0 :
    print('it is  ')
else :
    print('NO')

7-4 比薩配料 :編寫一個迴圈,提示使用者輸入一系列的比薩配料,並在使用者輸入'quit' 時結束迴圈。每當使用者輸入一種配料後,都列印一條訊息,說我們會在比薩中新增這種配料。

#7-4
while True:
    pizza = input('Please tell me the ingredients ')
    if pizza == 'quit':
        break;
    else:
        print('We would take the '+pizza+'in you pizza')

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

#7-5
while True :
    age = input("Please tell me your age: ")
    age= int(age)
    if age<3 :
        print("The price is 0")
    elif age>=3 and age<=12 :
        print("The price is 10 dollars")
    else :
        print("The price is 15 dollars")

7-6 三個出口 :以另一種方式完成練習7-4或練習7-5,在程式中採取如下所有做法。

在while 迴圈中使用條件測試來結束迴圈。

使用變數active 來控制迴圈結束的時機。

使用break 語句在使用者輸入'quit' 時退出迴圈。

#7-6
active = True
while active :
    age = input("Please tell me your age: ")
    if age=='quit':
        active = False
        break
    elif int(age)<3 :
        print("The price is 0")
    elif int(age)>=3 and int(age)<=12 :
        print("The price is 10 dollars")
    elif int(age)>12 :
        print("The price is 15 dollars")

7-7 無限迴圈 :編寫一個沒完沒了的迴圈,並執行它(要結束該迴圈,可按Ctrl +C,也可關閉顯示輸出的視窗)。

#7-7
while 1:
    print('HH')

7-8 熟食店 :建立一個名為sandwich_orders 的列表,在其中包含各種三明治的名字;再建立一個名為finished_sandwiches 的空列表。遍歷列表sandwich_orders ,對於其中的每種三明治,都列印一條訊息,如I made your tuna sandwich ,並將其移到列表finished_sandwiches 。所有三明治都製作好後,列印一條訊息,將這些三明治列出來。

#7-8
sandwich_orders = ['apple','orange','melon']
finished_sandwiches = []
while sandwich_orders :
    sandwich = sandwich_orders.pop();
    print('I made your '+sandwich+' sandwich')
    finished_sandwiches.append(sandwich)
print(finished_sandwiches)

7-9 五香菸薰牛肉(pastrami)賣完了 :使用為完成練習7-8而建立的列表sandwich_orders ,並確保'pastrami' 在其中至少出現了三次。在程式開頭附近新增這樣的程式碼:列印一條訊息,指出熟食店的五香菸薰牛肉賣完了;再使用一個while 迴圈將列表sandwich_orders 中的'pastrami' 都刪除。確認最終的列表finished_sandwiches 中不包含'pastrami' 。

#7-9
sandwich_orders = ['apple','orange','melon','pastrami','pastrami','pastrami']
finished_sandwiches = []
print('pasrtami賣完了!!')
while 'pastrami' in sandwich_orders :
    sandwich_orders.remove("pastrami")
while sandwich_orders :
    sandwich = sandwich_orders.pop();
    print('I made your '+sandwich+' sandwich')
    finished_sandwiches.append(sandwich)
print(finished_sandwiches)

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

#7-10
places={}
active = True
while active :
    name = input("\nWhat is your name ? ")
    place = input('If you could visit one place in the world ,where would you go? ')
    places[name] = place
    repeat = input('Would you like to let another person respond?(yes / no)')
    if repeat == 'no':
        active = False
print('result')
for name,place in places.items():
    print(name + ' would like to ' + place +'.')