博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python:从入门到实践--第七章--用户输入和while循环-练习
阅读量:4656 次
发布时间:2019-06-09

本文共 3098 字,大约阅读时间需要 10 分钟。

#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 = Truewhile 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 = Truewhile 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)

 

转载于:https://www.cnblogs.com/geeker-xjl/p/10635287.html

你可能感兴趣的文章
Android图片加载框架Glide用法
查看>>
域名解析
查看>>
callable()在python2中应用
查看>>
图片裁剪 cropper.js 上传组件封装 vue
查看>>
2017.10.21 Java中的数据源与连接池技术
查看>>
2018.5.8 XML编程
查看>>
2018.12.29 111
查看>>
2013年MBA、MPA、MPAcc入学考试英语辅导教材
查看>>
JavaScript高级程序设计:第2版(china-pub首发)
查看>>
HDU1512 ZOJ2334 Monkey King 左偏树
查看>>
BZOJ1823 [JSOI2010]满汉全席 2-sat
查看>>
Codeforces 1053C Putting Boxes Together 树状数组
查看>>
算法思维方式之二——DP与DFS
查看>>
网页版分享功能
查看>>
查找算法之二分查找法
查看>>
创建界面视图的流程
查看>>
微信公众平台体验(二)JS-SDK
查看>>
[Leetcode] Linked List Cycle
查看>>
第十九节(异常的基本概念, 异常的分类, 异常的捕获和处理,自定义异常,方法覆盖与异常)...
查看>>
齐头并进
查看>>