#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':
break
else:
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":
break
if 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=True
prompt='please print a series of toppings'
prompt+='if enouth print "quit" '
while active:
toppings=input(prompt)
if toppings=='quit':
active=False
else:
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=True
while active:
age=input(ask)
if age=="quit":
active=False
elif 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-8
sandwich_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-9
sandwich_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-10
responses={}
print('If you could visit one place in the world , where would you go?')
polling_active=True
while polling_active:
name=input('\n What is your name ')
response=input('where woulf you like to go someday?')
responses[name]=response
repeat=input('if you want another one to continue?(yes/no)')
if repeat=='no':
polling_active=False
print("--------polling results----------")
for name, response in responses.items():
print(name+" would like to go "+response.title())
while循环部分
#7.2while循环
current_number=1
while 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=True
while active:
message=input(prompt)
if message =='quit':
active=False
else:
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':
break
else:
print('i would like to go to '+city.title()+" !")
#在循环中使用continue
#通过continue使循环中部分符合条件的statement跳过循环执行
current_number=0
while current_number <10:
current_number +=1
if current_number%2==0:
continue
print(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=True
while polling_active:
name=input('What is your name?')
response=input('which mountaiin would you like to clime someday ')
responses[name]=response
repeat=input('would you like to let other person.(yes/no)')
if repeat=='no':
polling_active=False
print('\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':
break
else:
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":
break
if 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=True
prompt='please print a series of toppings'
prompt+='if enouth print "quit" '
while active:
toppings=input(prompt)
if toppings=='quit':
active=False
else:
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=True
while active:
age=input(ask)
if age=="quit":
active=False
elif 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-8
sandwich_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-9
sandwich_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-10
responses={}
print('If you could visit one place in the world , where would you go?')
polling_active=True
while polling_active:
name=input('\n What is your name ')
response=input('where woulf you like to go someday?')
responses[name]=response
repeat=input('if you want another one to continue?(yes/no)')
if repeat=='no':
polling_active=False
print("--------polling results----------")
for name, response in responses.items():
print(name+" would like to go "+response.title())