# 1)汽车租赁:编写一个程序,询问用户要租什么样的汽车,并打印一条消息,如下所示。Let me see if I can find you a Subaru.

car=input("What kind of car would you like to rent? ")

print(f"Let me see if I can find you a {car}.")

# 2)餐厅订位:编写一个程序,询问用户有多少人用餐。如果超过8个人,就打印一条消息,指出没有空桌;否则指出有空桌

number=int(input("How many people are dinning? "))

if number>8:

    print("There is no empty table.")

else:

    print("There is an empty table.")

# 3)10的整数倍:让用户输入一个数,并指出这个数是否是10的整数倍

number=int(input("Please input a number: "))

if number%10==0:

    print(f"{number} is a multiple of 10.")

else:

    print(f"{number} is not a multiple of 10.")

# 4)披萨配料:编写一个循环,提示用户输入一系列披萨配料,并在用户输入‘quit’时结束循环。每当用户输入一种配料时,都打印一条消息,指出我们会添加这种配料。

prompt="Please input the toppings you want for your pizza: "

prompt+="(Enter 'quit' when you are finished.)"

while True:

    topping=input(prompt)

    if topping=='quit':

        break

    else:

        print(f"I'll add {topping} to your pizza.")

# 5)电影票:有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3-12岁的观众收取10元票价;12及以上的观众收取15元票价。,请编写一个循环,在其中询问用户的年龄,并指出其票价

prompt="Please input your age: "

while True:

    age=input(prompt)

    age=int(age)

    if age<3:

        print("Your ticket is free.")

    elif age>=3 and age<12:

        print("Your ticket is 0.")

    else:

        print("Your ticket is $15.")    # 这里是死循环,所以下面的代码和这里要分开,你可以可自己添加跳出循环的语句

# 6)三种出路:以不同的方式完成第四题或第五题,在程序中采取如下做法。

#   6.1)在while循环中使用条件测试来结束循环

#   6.2)使用变量antive来控制循环结束的时机

#   6.3)使用break语句在用户输入quit时结束循环

prompt="Please input the toppings you want for your pizza: "

prompt+="(Enter 'quit' when you are finished.)"

active=True

while active:

    topping=input(prompt)

    if topping=='quit':

        active=False

    else:

        print(f"I'll add {topping} to your pizza.")

# 7)无限循环:编写一个没完没了的循环,并运行它。

# 这个比较简单,上面第五题就是一个死循环

# 8)熟食店:创建一个名为'sandwich_orders'的列表,在其中包含 various kinds of sandwiches,如'pastrami'、'tuna'、'ham'、'cheese'等。再创建一个名为'finished_sandwiches'的列表,用于存储制作好的三明治。遍历'sandwich_orders'列表,

#           将每种三明治都打印出来,并将其移到'finished_sandwiches'列表中。最后,打印一个消息,指出所有三明治都制作完毕。

sandwich_orders=['pastrami', 'tuna', 'ham', 'cheese']

finished_sandwiches=[]

while sandwich_orders:

    current_sandwich=sandwich_orders.pop()

    print(f"I made your {current_sandwich} sandwich.")

    finished_sandwiches.append(current_sandwich)

print(f"All sandwich orders: {finished_sandwiches}")

# 9)五香烟熏牛肉卖完了:使用第八题的列表ti、'ham'和'pastrami'来创建一个名为'sandwich_orders'的列表。再使用一个循环将三明治的订单打印出来,并指出五香烟熏牛肉卖完了。

sandwich_orders=['pastrami', 'tuna', 'ham', 'cheese', 'pastrami', 'pastrami']

while 'pastrami' in sandwich_orders:

    sandwich_orders.remove('pastrami')

print(sandwich_orders)

# 10)梦想中的度假胜地:编写一个程序,调查用户梦想中的度假胜地。使用类似于'If you could visit one place in the world, where would you go?'的提示,并编写一个打印调查结果的代码。

prompt="If you could visit one place in the world, where would you go? "

while True:

    place=input(prompt)

    if place=='quit':

        break

    else:

        print(f"I'd love to go to {place}.")

        continue


 

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐