今天为了去单位吃个早饭,六点半就起了,上了三个闹钟。我真是一个吃货。
啥也不说了,开了一天会,开始吧。
7-1 汽车租赁 :编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如“Let me see if I can find you a Subaru”。
实现:
car = input("What kind of cars do you want to rent?")
print("Let me see if I can find you a " + car)
运行结果
7-2 餐馆订位 :编写一个程序,询问用户有多少人用餐。如果超过8人,就打印一条消息,指出没有空桌;否则指出有空桌。
实现:
meal = int(input("How many people have meals?\n"))
if meal > 8:
print("There is no empty table!")
else:
print("There are empty tables.")
运行结果:
7-3 10的整数倍 :让用户输入一个数字,并指出这个数字是否是10的整数倍。
实现:
number = int(input("Please input a number: \n"))
if number % 10 == 0:
print("Is an integer multiple of 10!")
else:
print("Please try again!")
运行结果:
7-4 比萨配料 :编写一个循环,提示用户输入一系列的比萨配料,并在用户输入'quit' 时结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。
实现:
while True:
ingredients = input("What kind of ingredient do you want to add to?")
print("We will add to the " + ingredients.title() + " in the pizza!")
if ingredients == 'quit':
break
运行结果:
7-5 电影票 :有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。
实现:
while True:
age = int(input("Please input your age:\n"))
if age < 3:
print("The fare is free!")
elif age > 12:
print("The fare is $15.")
else:
print("The fare is $10.")
运行结果:
7-6 三个出口 :以另一种方式完成练习7-4或练习7-5,在程序中采取如下所有做法。 在while 循环中使用条件测试来结束循环。
- 使用变量active 来控制循环结束的时机。
- 使用break 语句在用户输入'quit' 时退出循环。
实现:
active = True
count = 0
while active:
age = int(input("Please input your age:\n"))
if age < 3:
print("The fare is free!")
elif age > 12:
print("The fare is $15.")
else:
print("The fare is $10.")
count += 1
if count > 3:
active = False
运行结果:
7-7无限循环 :编写一个没完没了的循环,并运行它(要结束该循环,可按Ctrl+C,也可关闭显示输出的窗口)。
实现:
while True:
print("fk you!")
运行结果:
明早要去很远的地方,早点睡,就到这里吧。







网友评论