您的位置:首页 > 编程语言 > Python开发

【日常python3-9】用户输入和while循环

2019-06-12 12:22 489 查看

任务1.汽车租赁

  1. 问题描述
    编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如“LetmeseeifIcan find you a Subaru”。
  2. 源代码
message = input("Please tell me how sorts of cars do you want:")
print("I like "+message+" car.")
  1. 测试结果

任务2.餐馆订位

  1. 问题描述
    编写一个程序,询问用户有多少人用餐。如果超过8人,就打印一条消息,指出没有空桌;否则指出有空桌。

  2. 源代码

number = input("How many of you are eating?")
number = int(number)
if number>8 :
print("Sorry! There are no tables available.")
else:
print("There are some tables available.")
  1. 测试结果

任务3.10的整数倍

  1. 问题描述
    让用户输入一个数字,并指出这个数字是否是10的整数倍。
  2. 源代码
number = input("Please input a number:")
number = int(number)
if number % 10 ==0 :
print("This number is an integer multiole of 10. ")
else:
print("This number is not an integer multiole of 10.")
  1. 测试结果

任务4.比萨配料

  1. 任务描述
    编写一个循环,提示用户输入一系列的比萨配料,并在用户输入’quit’ 时结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。

  2. 源代码

# coding:gbk
message = "\n请输入你需要的一系列比萨配料:"
message += "\n输入quit可以退出此程序!"
information = ""
while information != 'quit':
information = input(message)

if information != 'quit':
print("我们会在比萨中加入"+information+"。")
  1. 测试结果

任务5.电影票

  1. 问题描述
    有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用
    户的年龄,并指出其票价。
  2. 源代码
message = "Please tell me how you old:"
age = ""
while age !='quit':
age = input(message)
if int(age)<3:
print("Free!")
elif int(age)<12:
print("10 dollars!")
else:
print("12 dollars!")
  1. 测试结果

任务6.无限循环

  1. 问题描述
    程序陷入无限循环,可按Ctrl+ C,也可关闭显示程序输出的终端窗口。
  2. 源代码
x = 1
while x <= 5:
print(x)
  1. 测试结果


4. 分析
要避免编写无限循环,务必对每个while 循环进行测试,确保它按预期那样结束。如果希望程序在用户输入特定值时结束,可运行程序并输入这样的值;如果在这种情况下程序没有结束,请检查程序处理这个值的方式,确认程序至少有一个这样的地方能让循环条件为False 或让break 语句得以执行。有些编辑器(如Sublime Text)内嵌了输出窗口,这可能导致难以结束无限循环,因此不得不关闭编辑器来结束无限循环。

任务7.熟食店

  1. 问题描述
    创建一个名为sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为finished_sandwiches 的空列表。遍历列表sandwich_orders ,对于其中的每种三明治,都打印一条消息,如I made your tuna sandwich ,并将其移到列表finished_sandwiches 。所有三明治都制作好后,打印一条消息,将这些三明治列出来。
  2. 源代码
sandwich_orders = ['Bocadillo','Arepa','Franc','Smoked Meat']
finished_sandwiches = []
while sandwich_orders:
current_sandwich = sandwich_orders.pop()
print("I made your "+current_sandwich+".")
finished_sandwiches.append(current_sandwich)
print("\nThe following sandwiches have been accomplished:")
for finished_sandwiche in finished_sandwiches:
print(finished_sandwiche)
  1. 测试结果

任务8.五熏肉

  1. 问题描述
    创建列表sandwich_orders ,确保’pastrami’ 在其中至少出现了三次。在程序开头附近添加这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个while 循环将列表sandwich_orders 中的’pastrami’ 都删除。确认最终的列表finished_sandwiches 中不包含’pastrami’ 。
  2. 源代码
sandwich_orders = ['Bocadillo','pastrami' ,'Arepa','pastrami' ,'Franc','pastrami' ,'Smoked Meat']
print(sandwich_orders)
print("Our shop is out of pastrami!")
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
print(sandwich_orders)
  1. 测试结果

任务9.梦想的度假胜地

  1. 问题描述
    编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could visit one placein the world, where would you go?”的提示,并编写一个打印调查结果的代码块。
  2. 源代码
# coding:gbk
resorts = {}

# 设置一个标志,指出调查是否继续
polling_active = True

while polling_active:
# 提示输入被调查者的名字和回答
name = input("\nWhat is your name? ")
response = input("If you could visit one placein the world, where would you go?")

# 将答卷存储在在字典中
resorts[name] = response

# 看看是否还有人要参与调查
repeat = input("Would you like to let another person respond?")
if repeat == 'no':
polling_active = False

# 调查结束,显示结果
print("\n--- Poll Results ---")
for name,response in resorts.items():
print(name+" would like to go "+response+".")
  1. 测试结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: