天天看點

《Python程式設計 從入門到實踐》第七章習題選做

7-1 汽車租賃

message = input("Please enter what kind of car you want to buy: ")
print("Let me see if I can find you a " + message)
           

輸出:

Please enter what kind of car you want to buy: Benz
Let me see if I can find you a Benz
           

7-2 餐館訂位

num = int(input("How many people will have meals: "))
if num > 8:
    print("There is no empty table.")
else:
    print("There's an empty table here.")
           

輸出:

How many people will have meals: 9
There is no empty table.
           

7-3 10的整數倍

num = int(input("please input a num: "))
if num % 10 == 0:
    print(str(num) + " is an integer multiple of 10.")
else:
    print(str(num) + " is not an integer multiple of 10.")
           

輸出:

please input a num: 34
34 is not an integer multiple of 10.
           

7-4 比薩配料

msg = "Please enter a pizza ingredient, enter 'quit' to exit the program. Please enter: "
while True:
    msg1 = input(msg)
    if msg1 == 'quit':
        break
    else:
        print("We'll add " + msg1 + " in pizza.")
           

輸出:

Please enter a pizza ingredient, enter 'quit' to exit the program. Please enter: Tomatoes
We'll add Tomatoes in pizza.
Please enter a pizza ingredient, enter 'quit' to exit the program. Please enter: onion
We'll add onion in pizza.
Please enter a pizza ingredient, enter 'quit' to exit the program. Please enter: Olive oil
We'll add Olive oil in pizza.
Please enter a pizza ingredient, enter 'quit' to exit the program. Please enter: Black pepper
We'll add Black pepper in pizza.
Please enter a pizza ingredient, enter 'quit' to exit the program. Please enter: quit
           

7-8 熟食店

sandwich_orders = [
    "Ham sandwich", "Dried meat floss sandwich", "Bacon sandwich"
]
finished_sandwiches = []
while True:
    if len(sandwich_orders) != 0:
        sandwich = sandwich_orders.pop()
        print("I made your " + sandwich)
        finished_sandwiches.insert(0, sandwich)
    else:
        break
print(finished_sandwiches)
           

輸出:

I made your Bacon sandwich
I made your Dried meat floss sandwich
I made your Ham sandwich
['Ham sandwich', 'Dried meat floss sandwich', 'Bacon sandwich']
           

7-9 五香煙熏牛肉(pastrami)賣完了

sandwich_orders = [
    "Ham sandwich", "pastrami", "Dried meat floss sandwich", "pastrami",
    "Bacon sandwich", "pastrami"
]
finished_sandwiches = []
print("The spiced smoked beef in the deli was sold out.")
while "pastrami" in sandwich_orders:
    sandwich_orders.remove("pastrami")
print(sandwich_orders)
while True:
    if len(sandwich_orders) != 0:
        sandwich = sandwich_orders.pop()
        print("I made your " + sandwich)
        finished_sandwiches.insert(0, sandwich)
    else:
        break
print(finished_sandwiches)
           

輸出:

The spiced smoked beef in the deli was sold out.
['Ham sandwich', 'Dried meat floss sandwich', 'Bacon sandwich']
I made your Bacon sandwich
I made your Dried meat floss sandwich
I made your Ham sandwich
['Ham sandwich', 'Dried meat floss sandwich', 'Bacon sandwich']