#1.编写一个程序,询问用户要租赁什么样的汽车,并打印。
car = input("What's kind of cars dou you want to rent?,sir:")
print('Let me see if I can find you a '+ car) print('\n')
#2.编写一个程序,询问用户有多少人用餐。如果超过8人,就打印一条消息,指出没有空桌;否则指出有空桌
table = input("尊敬的先生/女士,请问订餐人数是多少?:")
if int(table) > 8:
print("非常抱歉,已经没有位置了")
else:
print("欢迎光临!") #3.10的倍数:让用户输入一个数字,并指出这个数字是否是10的倍数。
Ten_number = input("请输入一个数字:")
if int(Ten_number) % 10 == 0:
print('是10的倍数')
else:
print('不是10的倍数')
#1.编写一个程序,提示用户输入一系列的比萨配料,并在用户输入‘quit’时结束循环。
#每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料
charger_sheet = 'Please input some dosing you want:'
charger_sheet += "\nEnter 'quit' to end the program.\n" while messages != 'quit':
messages = input(charger_sheet)
if messages != 'quit':
print('we will add the '+messages+' to pizza') #2.电影票:有家电影院根据观众的年龄收取不同的票价:不到5岁的观众免费,5-12岁的观众为10美元,超过12岁的观众为15美元。
#编写一个循环,在其中询问用户的年龄,并指出其票价
prompt = '\nplease enter your age:'
prompt += "\nEnter 'quit' to exit the loop."
while True:
age = input(prompt)
if age == 'quit':
break
elif int(age) <5:
print('free')
elif 5 <= int(age) <12:
print('10 dollars.')
elif int(age) >= 12:
print('15 dollars.')
else:
print('Error!')
break #3.在while循环中使用条件测试来结束循环
#使用变量active来控制循环结束的时机
#使用break语句在用户输入‘quit’时退出循环 charger_sheet = 'Please input some dosing you want:'
charger_sheet += "\nEnter 'quit' to end the program.\n" active = True
while active:
messages = input(charger_sheet)
if messages == 'quit':
active=False
break
else:
print('we will add the '+messages+' to pizza') #1.熟食店:创建一个名为sandwich_orders的列表,其中包含各种三明治的名字。
#在创建一个名为finished_sandwiches的空列表,遍历列表sandwich_orders,对于其中的每种三明治,都打印一条消息
#并将其移至列表finished_sandwiches。所有三明治都制作好后打印一条消息,将这些三明治列出来。
sandwich_orders = ['milk','strawberry','blueberry','banana','pastrami']
finished_sanwiches = []
while sandwich_orders:
current_del = sandwich_orders.pop()
print('当前制作的三明治:' + current_del)
finished_sanwiches.append(current_del) print("\n所有制作号的三明治:")
for finish_sandwich in finished_sanwiches:
print(finish_sandwich) print('\n')
#2.牛肉卖完了:使用1中的列表sandwich_orders,并确保‘pastrami’在其中至少出现三次。在程序开头附近添加这样的代码:
#打印一条消息,指出牛肉买完了;再使用一个while循环将列表sandwich_orders中的‘pastrami’都删除。
#确认最终的列表finished_sandwiches不包含‘pastrami’
sandwich_orders = ['milk','pastrami','strawberry','pastrami','blueberry','banana','pastrami']
print( sandwich_orders)
print('牛肉卖完了') while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami') print(sandwich_orders) #3.度假胜地:编写一个程序,调查用户梦想的度假胜地。使用类似于‘if you could visit one place in the world, where would you go?’
#并编写一个打印调查结果的代码块
places = {}
active = True while active:
name = input("\nwhat's your name?")
response = input("If you could visit one place in the world,where would you go?")
places[name] = response next_question = input('Can you tell me another interesting things?(yes/no)')
if next_question == 'no':
active = False print("\nThe results of researches:")
for name,response in places.items():
print(name + ' would like to ' + response)

Python:从入门到实践--第七章--用户输入和while循环-练习的更多相关文章

  1. 第七章 用户输入和while循环

    7.1函数input()的工作原理 函数默认输入为字符串string,如果需使用数字,需用int进行类型转换 7.2 while循环 while是根据条件的真假判断是否进入执行 使用标志: 使用bre ...

  2. 第七章 用户输入和while 循环

    7.1 创建多行字符串的方式: 01 prompt="if you tell me who you are, we can personalize the message you see.& ...

  3. python从入门到实践-7章用户输入和while循环

    #!/user/bin/env python# -*- coding:utf-8 -*- # input() 可以让程序暂停工作# int(input('please input something: ...

  4. 第七章 用户输入和while语句

    大多数编程都旨在解决最终用户的问题,为此通常需要从用户那里获取一些信息.例如,假设有人要判断自己是否到了投票的年龄,要编写回答这个问题的程序,就需要知道用户的年龄,这样才能给出答案.因此,这种程序需要 ...

  5. Python:从入门到实践--第四章--列表操作--练习

    #1.想出至少三种你喜欢的水果,将其名称存储在一个列表中,再使用for循环将每种水果的名称都打印出来. #要求:(1)修改这个for循环,使其打印包含名称的句子,而不是仅仅是水果的名称.对于每种水果, ...

  6. Python:从入门到实践--第三章--列表简介--练习

    #1.将一些朋友的姓名存储在一个列表中,并将其命名为friends.依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来. #2.继续使用1中的列表,为每人打印一条消息,每条消息包含相同的问候语 ...

  7. Python:从入门到实践--第六章--字典--练习

    #1.人:使用一个字典来存储一个熟人的信息;包括姓,名,年龄和居住的城市.将字典中的每项信息都打印出来 friend = { 'last_name':'马', 'first_name':'脑壳', ' ...

  8. python 从入门到实践 第三章

    在第3章,你将学习如何在被称为列表的变量中存储信息集,以及如何通过遍历列表来操作其中的信息 写注释 # 代码越长 标识好代码的重要性 越来越重要要求习惯:在代码中编写清晰,简洁的注释开始研究更复杂的主 ...

  9. Python:从入门到实践--第十一章--测试代码--练习

    #1.城市和国家:编写一个函数,它接受两个形参:一个城市名和一个国家名. #这个函数返回一个格式为City,Country的字符串,如Santiago,Chile.将这个函数 #存储在一个名为city ...

随机推荐

  1. unistd.h

    unistd.h是unix std的意思,是POSIX标准定义的unix类系统定义符号常量的头文件, 包含了许多UNIX系统服务的函数原型 unistd.h在unix中类似于window中的windo ...

  2. day21_python_1124

    01 昨日内容回顾 类与类之间的关系: 依赖关系:将一个类的对象或者类名传到另一个类中. 关联关系 组合关系:将一个类的对象封装到另一个类的对象属性中. 聚合关系 boy gril school te ...

  3. python turtle库的几个小demo

    一.先上图 一个同切圆和五角星 上代码 import turtle #同切圆 turtle.pensize(2) turtle.circle(10) turtle.circle(40) turtle. ...

  4. 10ci

  5. SpringBoot项目搭建与打包

    一.环境准备 本地java环境jdk1.8 Maven版本3.5.2 IDE工具idea2017 二.SpringBoot微服务搭建 1.点击File >> New >> Pr ...

  6. 在win系统安装Git

    Git是优秀, 先进的代码版本控制管理工具, 是分布式, 比SVN进步. 比如我们可以从Github拉取代码, 或者上传到GIthub. 下面说下安装: 搜索引擎搜索Git, 找到官网, 找到安装文件 ...

  7. Possibly two send backs are happening for the same request

    错误 wso2 WARN {org.apache.synapse.transport.passthru.SourceHandler} -  Illegal incoming connection st ...

  8. Modelsim仿真.do脚本示例

    #“#”为注释 #删除原有工程,需重启Modelsim #vdel -all -lib work #退出当前仿真 quit -sim #清空命令行显示 .main clear #创建库,是实际存在的物 ...

  9. python builtin列表

    Python Builtin function获得通过 python3 -c "import builtins;ff=open('test.txt','w');strlist=[(i+'\n ...

  10. iOS Build Apps for the World WWDC

    Programming Resources https://developer.apple.com/internationalization/ WWDC Session Videos Internat ...