python入門練習第七章7 1 7 10

2021-09-10 05:38:24 字數 4620 閱讀 2906

僅作練習記錄,如有錯誤歡迎指正。

7-1 汽車租賃 : 編寫乙個程式, 詢問使用者要租賃什麼樣的汽車, 並列印一條訊息, 如「let me see if i can find you a subaru」。

car = input('what kind of car would you like to rent? ')

print('let me see if i can find you a '+car.title()+'.')

7-2 餐館訂位 : 編寫乙個程式, 詢問使用者有多少人用餐。 如果超過8人, 就列印一條訊息, 指出沒有空桌; 否則指出有空桌。

reservation = input('how many people come for eat? ')

reservation = int(reservation)

if reservation > 8:

print('sorry,it has not table.')

else:

print('it has empty table now.')

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

numb = input('please input a number,i will tell you the number is multiplier of ten! ')

numb = int(numb)

if numb % 10 == 0:

print(str(numb)+' is multiplier of ten!')

else:

print(str(numb)+' is not multiplier of ten!')

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

toppings = '\nplease add your pizza ingredients.'

toppings += "\n(enter 'quit' when you are finished.) "

while true:

topping = input(toppings)

if topping == 'quit':

break

else:

print('i will add '+topping.title()+' in your pizza.')

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

print("enter 'quit' when you are finished.")

prices = '\n請問您多大呢?我們是根據年齡段收費的。'

while true:

age = input(prices)

if age == 'quit':

break

elif int(age) < 3:

print('it is for free.')

elif int(age) <=12:

print('it costs 10 dollars.')

else:

print('it costs 15 dollars.')

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

toppings = '\nplease add your pizza ingredients.'

toppings += "\n(enter 'quit' when you are finished.) "

#active = true

#while active:

# topping = input(toppings)

# if topping == 'quit':

# active = false

# else:

# print('i will add '+topping.title()+' in your pizza.')

topping = ""

while topping != 'quit':

topping = input(toppings)

if topping != 'quit':

print('i will add ' + topping.title() + ' in your pizza.')

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

sandwich_orders = ['tuna','cuban','chicken']

finished_sandwiches =

for order in sandwich_orders:

print('i made your '+order.title()+' sandwich.')

print('finished sandwich is:')

for finish in finished_sandwiches:

print('\t'+finish)

while sandwich_orders: #方法二

finished_sandwich = sandwich_orders.pop()

print('i made your '+finished_sandwich.title()+' sandwich.')

print('finished sandwich is:')

for finish in finished_sandwiches:

print(finish)

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

sandwich_orders = ['tuna','cuban','pastrami','pastrami','chicken']

finished_sandwiches =

print('pastrami sandwiches are sold out.')

#while 'pastrami' in sandwich_orders:

# sandwich_orders.remove('pastrami')

#for sandwich_order in sandwich_orders:

#print(finished_sandwiches)

while sandwich_orders:

sandwich = sandwich_orders.pop()

if sandwich == "pastrami":

continue

else:

print(finished_sandwiches)

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

print('if you could visit one place in the world,where would you go?')

vacationlands = {}

places =

name = input('\nwhat is your name?')

while 1:

place = input('where is your vacationland?')

vacationlands[name] = places

otherplace = input('would you like other vacationland?(yes/no)')

if otherplace == 'no':

break

print('\n---poll results---')

for name,places in vacationlands.items():

print(name.title()+' likes to visit place is:')

for plac in places:

print(plac.title())

python第七章 python教程(第七章)

字典和集合 字典是python中唯一,乙個對映型別 如何建立乙個字典,如下 dict dict 滲透 網路安全 怎麼理解字典呢?現實生活中的字典可以通過首字母進行查詢要查詢的漢子,python也可以這樣理解,通過 前的元素查詢到冒號後的元素。為什麼說字典是唯一乙個對映型別呢?看圖。對映型別區別與序列...

Python核心程式設計 練習 第七章

7 1.字典方法。哪個字典方法可以用來把兩個字典合併到一起?可以通過update來更新。dict1 dict2 dict1.update dict2 dict1 7 3.字典和列表的方法。a 建立乙個字典,並把這個字典中的鍵按照字母順序顯示出來。b 現在根據已按照字母順序排序好的鍵,顯示出這個字典中...

瘋狂Python講義第七章練習

coding utf 8 created on 2020 01 28 author insisted search from traceback import print exc 1.提示使用者輸入乙個 n 表示使用者接下來要輸入 n 個宇符串,程式嘗試將使用者輸入的每乙個 字串 用 空格分割成兩個...