Python編程:從入門到實踐——【作業】——第七章(輸入和while循環)


第七章作業

7-1 汽車租賃 : 編寫一個程序, 詢問用戶要租賃什么樣的汽車, 並打印一條消息, 如“Let me see ifI can find you a Subaru”。
7-2 餐館訂位 : 編寫一個程序, 詢問用戶有多少人用餐。 如果超過8人, 就打印一條消息, 指出沒有空桌; 否則指出有空桌。
7-3 10的整數倍 : 讓用戶輸入一個數字, 並指出這個數字是否是10的整數倍。
答:7-1

prompt = 'What kind of car would you like to rent?'
car = input(prompt)
print('\n“Let me see ifI can find you a ' + car + '”。')

輸出:
What kind of car would you like to rent?Subaru

“Let me see ifI can find you a Subaru”。

 

 

 

7-2

number = input('How many people are eating?')
number = int(number)
if number >= 9:
    print('\n There have not tables')
else:
    print('\n There have a table')

輸出:
How many people are eating?8

 There have a table

 

 

7-3

方法1

number = input('Please input a diget:')
number = int(number)#,有這個不行啊,為什么呢
if number % 10 == 0:#這里的number帶()的
    print(str(number) + ' 這個數字是10的整數倍')
else:
    print(str(number) + ' 這個數字不是10的整數倍')#這里的number 要加str

輸出:
Please input a diget:5
5 這個數字不是10的整數倍

 

方法2

number = input('Please input a diget:')
#number = int(number),有這個不行啊,威懾呢么
if int(number) % 2 == 0:#這里的number帶()的
    print(number + ' 這個數字是10的整數倍')
else:
    print(number + ' 這個數字不是10的整數倍')

輸出;
Please input a diget:5
5 這個數字不是10的整數倍

 

 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, 也可關閉顯示輸出的窗口) 。

 7-4

prompt = "\nPlease Enter a series of pizza toppings: "
prompt += "\nWe will add this ingredient to our pizza. "
message = ''
while message != 'quit':
    message = input(prompt)
    if message != 'quit':
        print(message)

輸出;
Please Enter a series of pizza toppings: 
We will add this ingredient to our pizza. banana
banana

Please Enter a series of pizza toppings: 
We will add this ingredient to our pizza. chese
chese

Please Enter a series of pizza toppings: 
We will add this ingredient to our pizza. quit

 

 

 7-5

print('Cinemas will receive different fares based on viewer age:')
tickets = '\n Please,tell me how old are you:'
age = ''
active = True
while age != 'quit':
   age = input(tickets)
   if age == 'quit':
       active = False
   elif int(age) < 3:
       print('free')
   elif int(age) < 12:
       print(10)
   else:
       print(15)

輸出:
Cinemas will receive different fares based on viewer age:

 Please,tell me how old are you:2
free

 Please,tell me how old are you:10
10

 Please,tell me how old are you:15
15

 Please,tell me how old are you:quit

 

 

 

 7-6 用break語句:

print('Cinemas will receive different fares based on viewer age:')
tickets = '\n Please,tell me how old are you:'
age = ''
while True:
   age = input(tickets)
   if 'quit'in age:
       print('quit')
       break
   elif int(age) < 3:
       print('free')
   elif int(age) < 12:
       print(10)
   else:
       print(15)


輸出:
Cinemas will receive different fares based on viewer age:

 Please,tell me how old are you:2
free

 Please,tell me how old are you:16
15

 Please,tell me how old are you:quit
quit

 

 

 7-7

active = True
while active:
     print('hello world')

7-8 熟食店 : 創建一個名為sandwich_orders 的列表, 在其中包含各種三明治的名字; 再創建一個名為finished_sandwiches 的空列表。 遍歷列
表sandwich_orders , 對於其中的每種三明治, 都打印一條消息, 如I made your tuna sandwich , 並將其移到列表finished_sandwiches 。 所有三明
治都制作好后, 打印一條消息, 將這些三明治列出來。
7-9 五香煙熏牛肉( pastrami) 賣完了 : 使用為完成練習 7-8而創建的列表sandwich_orders , 並確保' pastrami' 在其中至少出現了三次。 在程序開頭附近添加
這樣的代碼: 打印一條消息, 指出熟食店的五香煙熏牛肉賣完了; 再使用一個while 循環將列表sandwich_orders 中的' pastrami' 都刪除。 確認最終的列
表finished_sandwiches 中不包含' pastrami' 。
7-10 夢想的度假勝地 : 編寫一個程序, 調查用戶夢想的度假勝地。 使用類似於“Ifyou could visit one place in the world, where would you go?”的提示, 並編寫一個打印調查
結果的代碼塊。

7-8

sandwich_orders = ['guita','chess','banana']
finished_sandwiches= []
while sandwich_orders:
     current_sandwich = sandwich_orders.pop()
     print('I made your tuna ' + current_sandwich)
     finished_sandwiches.append(current_sandwich)
print('所有三明治都制作好了')
for current_sandwich in finished_sandwiches:
     print(current_sandwich)

輸出;
I made your tuna banana
I made your tuna chess
I made your tuna guita
所有三明治都制作好了
banana
chess
guita

 

 7-9

sandwich_orders = ['guita','pastrami','chess','pastrami','banana','pastrami' ]
print('熟食店的五香煙熏牛肉(pastrami)都賣完了')
while 'pastrami' in sandwich_orders:#一定要注意空格,錯了好多次了
     sandwich_orders.remove('pastrami')#必須加括號啊
print(sandwich_orders)


輸出;
熟食店的五香煙熏牛肉(pastrami)都賣完了
['guita', 'chess', 'banana']

 

 7-10

responses = {}
dream_resort = True
while dream_resort:
     name = input("\nWhat is your name? ")
     response = input('“Ifyou could visit one place in the world, where would you go?”')
     responses[name] = response  #這將答卷存儲在字典中,如果是responses[response] = name,就是字典中鍵與值顛倒過來
     repeat = input("Would you like to let another person respond? (yes/ no) ")#這有了這個input就不算等於啊,算等待輸入
     if repeat == 'no' :
          dream_resort = False
print("\n--- Poll Results ---")
for name,response in responses.items():
     print(name + "would like visit " + response + "in the world . ")


輸出:What is your name? 曹富
“Ifyou could visit one place in the world, where would you go?”長城
Would you like to let another person respond? (yes/ no) yes

What is your name? 曹輝
“Ifyou could visit one place in the world, where would you go?”
Would you like to let another person respond? (yes/ no) no --- Poll Results ---
曹富would like visit 長城in the world . 
曹輝would like visit 家in the world . 

 

 

 證明responses[name] = response,這是一種將信息儲存到列表中的形式 

 

responses = {}
name = 'ni'#Python中的key 不可以是 list類型  因為 list是可變的  不能被hash
response = ['ta']
responses[name] = response
print(responses)

輸出:
{
'ni': ['ta']}

 


免責聲明!

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



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