第7章python作業

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

number=input("How many people? Please:")
num=int(number)
if num>8:
    print("Sorry, There is no empty table.")
else:
    print("We have empty table")

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

number=input("Please input a number:")
num=int(number)
if num%10==0:
    print("This number is an integer multiple of 10.")
else:
    print("This number is not an integer multiple of 10.")

7-4 比薩配料 :編寫一個循環,提示用戶輸入一系列的比薩配料,並在用戶輸入'quit' 時結束循環。每當用戶輸入一種配料後,都打印一條消息,說我們會在比薩中添加這種配料。

while True:
    st=input("Please input an ingredient:");
    if (st=='quit'):
        break
    print("We will add "+st+" to the pizza")

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

num=0
while True:
    num=num+1
    print(num)
    

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

sandwich_orders=['tuna','ham','fish','eggplant','peanut butter']
findished_sandwich=[]
while sandwich_orders:
    now=sandwich_orders.pop()
    print("I made your "+now+" sandwich")
    findished_sandwich.append(now)
print("We have made these sandwich:")
print(findished_sandwich)

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

sandwich_orders=['pastrami','tuna','pastrami','pastrami','ham','fish','eggplant','peanut butter']
findished_sandwich=[]
print("Pastrami is sold out")
while 'pastrami' in sandwich_orders:
    sandwich_orders.remove('pastrami')
print(sandwich_orders)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章