python编程 从入门到实践第七章 输入与while循环

来源:互联网 发布:改号软件 编辑:程序博客网 时间:2024/05/20 13:07
#char 7 用户输入和while循环message=input('tell me something, and i will repeat it to you:')print(message)#input接受一个参数,即要向用户显示的提示或者说明,即用户可以看到引号中的内容再进行输入#输入内容储存再变量中再进行输出name=input('please enter your name: ')print('hello'+name+"!")prompt="if you tell us who you are ,we can personalize the message you see."prompt += "\n what is your first name?"name= input(prompt)print("\n hello, "+name+"!")#使用Int()来获取数值输入age=input('how old are you?')age=int(age)print(age>=20)#即是通过输入的都是以字符串的形式,若是以字符串的形式比大小会报错,用Int转换格式height=input(' how tall are you , in inches?')height=int(height)if height>=36:print('\n you are tall enough to ride!')else:print('\n you will be able to ride when you are a liittle older')#求模运算符print(4%3)print(5%3)#通过输入判断奇数或者偶数number=input('enter a number, and i will tell you if it is even or odd')number=int(number)if number%2==0:print('the number'+str(number)+'is even')else:print('the number'+str(number)+'is odd')#7-4:prompt='please print a series of toppings'prompt+='if enouth print "quit" 'while True:toppings=input(prompt)if toppings=='q':breakelse:print('we will add'+toppings+'into your pizza')#7-5:ask='How old are you ?'ask+="input 'quit' if you want to end"while True:age=input(ask)if age=="quit":breakif int(age)<3:print('you are free for the movies')elif int(age)<12:print('you shoulf cost 10 dollors for the movies')else:print('you shoulf cost 15 dollors for the movies')#7-6(1):active=Trueprompt='please print a series of toppings'prompt+='if enouth print "quit" 'while active:toppings=input(prompt)if toppings=='quit':active=Falseelse:print('we will add '+toppings+' into your pizza')#7-6(2):ask='How old are you ?'ask+="input 'quit' if you want to end"active=Truewhile active:age=input(ask)if age=="quit":active=Falseelif int(age)<3:print('you are free for the movies')elif int(age)<12:print('you shoulf cost 10 dollors for the movies')else:print('you shoulf cost 15 dollors for the movies')#7-8sandwich_order=['pork','chiceken','beef','eeg']finised_sandwiches=[]while sandwich_order:current_sandwich=sandwich_order.pop()print("i made your sandwich with"+current_sandwich)finised_sandwiches.append(current_sandwich)print(finised_sandwiches)#7-9sandwich_order=['pork','psatrami','chiceken','psatrami','beef','psatrami','eeg']print('the psatrami has sold old ')while 'psatrami' in sandwich_order:sandwich_order.remove('psatrami')print(sandwich_order)# 7-10responses={}print('If you could visit one place in the world , where would you go?')polling_active=Truewhile polling_active:name=input('\n What is your name ')response=input('where woulf you like to go someday?')responses[name]=responserepeat=input('if you want another one to continue?(yes/no)')if repeat=='no':polling_active=Falseprint("--------polling results----------")for name, response in responses.items():print(name+" would like to go "+response.title())

while循环部分

#7.2while循环current_number=1while current_number<=5:print(current_number)current_number += 1#用户选择何时退出#后采用message != 'quit' 就不打印message信息,防止每条都打印了message的信息prompt='tell me something, and i will repeat it to you:'prompt+='\n enter a quit if you want to end'message=" "while message !='quit':message=input(prompt)if message !='quit':print(message)#使用标志与用break退出循环#将active设置为True 和 False 的状态变量,通过根据其值来决定while的判断prompt='tell me something, and i will repeat it to you:'prompt+='\n enter a quit if you want to end'active=Truewhile active:message=input(prompt)if message =='quit':active=Falseelse:print(message)prompt='\n please tnter the name of a city you have visited:'prompt='\n (enter quit when you finish.)'while True:city=input(prompt)if city=='quit':breakelse:print('i would like to go to '+city.title()+" !")#在循环中使用continue#通过continue使循环中部分符合条件的statement跳过循环执行current_number=0while current_number <10:current_number +=1 if current_number%2==0:continueprint(current_number)#用while处理列表和字典#在列表中之间移动元素unconfirmed_users=['alice','brain','candace']confirmed_users=[]while unconfirmed_users:current_user=unconfirmed_users.pop()print("Verifying user: "+current_user.title())confirmed_users.append(current_user)print("\n the following users have been confirmed")for confirmed_user in confirmed_users:print(confirmed_user.title())#删除包含特定的元素pets=['dog','cat','dog','goldfish','cat','rabbit','cat']print(pets)while 'cat' in pets:pets.remove('cat')#使用这用户输入填充字典responses={}polling_active=Truewhile polling_active:name=input('What is your name?')response=input('which mountaiin would you like to clime someday ')responses[name]=responserepeat=input('would you like to let other person.(yes/no)')if repeat=='no':polling_active=Falseprint('\n--------poll results-----')for name,response in responses.items():print(name+'would you like to climb '+response+".")

课后练习

课后题由7-4开始#7-4:prompt='please print a series of toppings'prompt+='if enouth print "quit" 'while True:toppings=input(prompt)if toppings=='q':breakelse:print('we will add'+toppings+'into your pizza')#7-5:ask='How old are you ?'ask+="input 'quit' if you want to end"while True:age=input(ask)if age=="quit":breakif int(age)<3:print('you are free for the movies')elif int(age)<12:print('you shoulf cost 10 dollors for the movies')else:print('you shoulf cost 15 dollors for the movies')#7-6(1):active=Trueprompt='please print a series of toppings'prompt+='if enouth print "quit" 'while active:toppings=input(prompt)if toppings=='quit':active=Falseelse:print('we will add '+toppings+' into your pizza')#7-6(2):ask='How old are you ?'ask+="input 'quit' if you want to end"active=Truewhile active:age=input(ask)if age=="quit":active=Falseelif int(age)<3:print('you are free for the movies')elif int(age)<12:print('you shoulf cost 10 dollors for the movies')else:print('you shoulf cost 15 dollors for the movies')#7-8sandwich_order=['pork','chiceken','beef','eeg']finised_sandwiches=[]while sandwich_order:current_sandwich=sandwich_order.pop()print("i made your sandwich with"+current_sandwich)finised_sandwiches.append(current_sandwich)print(finised_sandwiches)#7-9sandwich_order=['pork','psatrami','chiceken','psatrami','beef','psatrami','eeg']print('the psatrami has sold old ')while 'psatrami' in sandwich_order:sandwich_order.remove('psatrami')print(sandwich_order)# 7-10responses={}print('If you could visit one place in the world , where would you go?')polling_active=Truewhile polling_active:name=input('\n What is your name ')response=input('where woulf you like to go someday?')responses[name]=responserepeat=input('if you want another one to continue?(yes/no)')if repeat=='no':polling_active=Falseprint("--------polling results----------")for name, response in responses.items():print(name+" would like to go "+response.title())



阅读全文
0 0
原创粉丝点击