第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)

你可能感兴趣的:(第7章python作业)