Python:从入门到实践--第七章--用户输入和while循环-练习
#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循环-练习的更多相关文章
- 第七章 用户输入和while循环
7.1函数input()的工作原理 函数默认输入为字符串string,如果需使用数字,需用int进行类型转换 7.2 while循环 while是根据条件的真假判断是否进入执行 使用标志: 使用bre ...
- 第七章 用户输入和while 循环
7.1 创建多行字符串的方式: 01 prompt="if you tell me who you are, we can personalize the message you see.& ...
- python从入门到实践-7章用户输入和while循环
#!/user/bin/env python# -*- coding:utf-8 -*- # input() 可以让程序暂停工作# int(input('please input something: ...
- 第七章 用户输入和while语句
大多数编程都旨在解决最终用户的问题,为此通常需要从用户那里获取一些信息.例如,假设有人要判断自己是否到了投票的年龄,要编写回答这个问题的程序,就需要知道用户的年龄,这样才能给出答案.因此,这种程序需要 ...
- Python:从入门到实践--第四章--列表操作--练习
#1.想出至少三种你喜欢的水果,将其名称存储在一个列表中,再使用for循环将每种水果的名称都打印出来. #要求:(1)修改这个for循环,使其打印包含名称的句子,而不是仅仅是水果的名称.对于每种水果, ...
- Python:从入门到实践--第三章--列表简介--练习
#1.将一些朋友的姓名存储在一个列表中,并将其命名为friends.依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来. #2.继续使用1中的列表,为每人打印一条消息,每条消息包含相同的问候语 ...
- Python:从入门到实践--第六章--字典--练习
#1.人:使用一个字典来存储一个熟人的信息;包括姓,名,年龄和居住的城市.将字典中的每项信息都打印出来 friend = { 'last_name':'马', 'first_name':'脑壳', ' ...
- python 从入门到实践 第三章
在第3章,你将学习如何在被称为列表的变量中存储信息集,以及如何通过遍历列表来操作其中的信息 写注释 # 代码越长 标识好代码的重要性 越来越重要要求习惯:在代码中编写清晰,简洁的注释开始研究更复杂的主 ...
- Python:从入门到实践--第十一章--测试代码--练习
#1.城市和国家:编写一个函数,它接受两个形参:一个城市名和一个国家名. #这个函数返回一个格式为City,Country的字符串,如Santiago,Chile.将这个函数 #存储在一个名为city ...
随机推荐
- 版本管理工具Git(2)git的使用
上一篇带大家认识了Git,在本篇中将讲解Git的安装及使用: Git系列导航 版本管理工具Git(1)带你认识git 版本管理工具Git(2)git的安装及使用 版本管理工具Git(3)VS下如何使用 ...
- 将VS项目提交至SVN时,怎样忽略bin和obj目录中的文件
方法一: 通过设置SVN的Global ignore pattern值. 使用下面的设定值: *.o *.lo *.la *.al .libs *.so *.so.[0-9]**.a *.pyc *. ...
- 在kali linux上安装VMware tool
在安全圈的门口徘徊了一年,一直不知道该如何入门,现在决定先从kali 入手.有同样兴趣的伙伴欢迎一起. 但是刚在VMware上安好系统就遇到了一个大麻烦,看了很多书,还有教程但总是遇到这样那样的问题. ...
- unity中EventTrigger组件的应用
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using D ...
- element-ui的那些坑与总结
tags: 默认情况下,下划线是文本宽度 如果要加宽,则可以设置文本(label)的padding, 常规情况下,无法改label宽度,因为他是动态计算的 不过,可以通过自定义,把label拿出来,自 ...
- 小学四则运算编程(c#)
---恢复内容开始--- 预计耗时与实际耗时: 代码基本完善后,对代码进行了性能改善,使用递归并减少了一些不必要的代码. 项目分析:二年级以下无乘除,四年级以下无小数 性能: 类图: 通过这次个人项目 ...
- centos7搭建日志服务器
服务器端 修改配置文件 /etc/rsyslog.conf中 $ModLoad imudp $UDPServerRun 514 打开日志服务监听 修改/etc/sysconfig/rsyslog文件 ...
- Ubuntu16.04 ionic(jdk,sdk,gradle)环境搭建完全攻略
在Ubuntu16.04当中搭建一个ionic环境还是按照官方教程的来,主要问题是首先要把JDK,SDK搭好,环境变量配好.本文中给的包的下载请不要直接用浏览器下载,很慢,尽量用wget 下载,重要的 ...
- hosts 添加主机和ip映射
背景:在服务器搭建环境时,迁移项目后,停止服务出现以下问题. 原因:项目停止或启动找不到映射名称或服务. 解决:修改hosts文件.hosts文件地址 /etc/hosts 添加主机和 ...
- 每天一点Linux系列之—vim