#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. C# 加载并显示菜单

    1,支持cui和cuix. 2,菜单组重复加载或显示,C#下都会崩溃.所以要判断. 3,菜单加到最后. public static AcadMenuGroup LoadMenu(AcadMenuGro ...

  2. 常被问到的十个 Java 面试题

    在这篇文章中,我试图收录最有趣和最常见的问题.此外,我将为您提供正确的答案. 接下来,就让我们来看看这些问题. 1. 以满分十分来评估自己——你有多擅长 Java? 如果你并不完全确信你自己或是你对 ...

  3. 开发部署项目时出现:java.lang.OutOfMemoryError: PermGen space

    java.lang.OutOfMemoryError: PermGen space 错误: 原文地址:http://www.cnblogs.com/shihujiang/archive/2012/06 ...

  4. Html+css学习笔记二 标题

    学习新标签,标题 <html> <head> <title>tags</title> </head> <body> <h1 ...

  5. GIT 生成公钥

    ssh-keygen -t rsa -C "xxxxx@xxxxx.com" cat ~/.ssh/id_rsa.pub

  6. 2ci

  7. java AQS(AbstractQueuedSynchronizer)同步器详解

    除了内置锁(synchronized)外,java AQS(AbstractQueuedSynchronizer)同步器几乎是所有同步容器,同步工具类的基础.ReentrantLock.Reentra ...

  8. 获取百度地图POI数据二(准备搜索关键词)

    上篇讲到  想要获取尽可能多的POI数据 需要准备尽可能多的搜索关键字   那么这些关键字如何得来呢?   本人使用的方法是通过一些网站来获取这些关键词   http://poi.mapbar.com ...

  9. [c++]关于strcpy函数溢出解决方案

    必须包含的头文件:<string.h> 可改写成安全函数strcpy_s 找到[项目属性],点击[C++]里的[预处理器],对[预处理器]进行编辑,在里面加入一段代码:_CRT_SECUR ...

  10. python之常用模块学习

    1.模块调用 import module from module import xx from module.xx.xx import xx as rename from module.xx.xx i ...