六、用户输入和while循环
1.input函数
message=input("Tell me something,and I will repeat it back to you:")
print(message)prompt="If you tell us who you are ,we can personalize the message you see."
prompt+="\nWhat is your first name?"
name=input(prompt)
print("Hello,"+name.title())2.使用int函数更改输入
age=input("How old are you ?")
age=int(age)
if age>=18:
print("\nYour are old enough to drink! ")#求模运算符 %两数相除并返回余数
number=int(input("Enter a number ,and I'll tell you if it's even or odd:"))
if number%2==0:
print("\nThe number "+str(number)+" ,it's even")
else:
print("\nThe number "+str(number)+" ,it's odd.")3.while循环
current_number=1
while current_number<=5
print(current_number)
current_number+=14.让用户选择何时退出
prompt="\nTell me something and I will repeat it back to you :"
prompt+="\nEnter 'quit'to end the program."
message=""
while message!='quit':
message=input(prompt)
if message!='quit':
print(message)#使用标志 因为很多情况导致循环推出,因此使用标志来作为推出循环的标志
prompt="\nTell me something and I will repeat it back to you :"
prompt+="\nEnter 'quit'to end the program."
active=True
while active:
message=input(prompt)
if message=='quit':
active=False
else:
print(message)#使用break推出循环
prompt="\nPlease enter the name of a city you have visited :"
prompt+="\n(Enter 'quit' when you are finished)."
while True:
city=input(prompt)
if city=='quit':
break
else:
print("I'd love to go to "+city.title()+"!")#在循环中使用continue
current_number=0
while current_number<10:
current_number+=1
if current_number%2==0:
continue
print(current_number)5.while循环处理字典和列表
#在列表间移动元素
unconfirmed_users=['alice','brian','candace']
confirmed_users=[]
while unconfirmed_users:
current_user=unconfirmed_users.pop()
print("Verifying user:"+current_user.title())
confirmed_users.append(current_user)
print("\nThe 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')
print(pets)#使用用户输入来填充字典
responses={}
polling_active=True
while polling_active:
name=input("\nWhat is your name?")
response=input("Which mountain would you like to climb someday?")
responses[name]=response
repeat=input("Would you like to let another person respond?(yes/no)")
if repeat=='no':
polling_active=False
print("\n---Poll Results---")
for name,response in responses.items():
print(name+"would like to climb "+response +".")八、函数
1.定义函数
def greet_user():
print("hello")
greet_user()2.向函数传递信息
def greet_user(username):
print("hello "+username.title()+"!")
username=input("\nPlease enter your name:")
greet_user(username)#传递实参
def describe_pet(animal_type,pet_name):
print("\nI have a"+animal_type+".")
print("My "+animal_type+"'s name is "+pet_name.title()+".")
describe_pet('hamster','harry')
describe_pet('dog','Willie')#调用函数多次
describe_pet(animal_type='hamaster',pet_name='harry') #关键字实参,准确指定函数中定义的形参名
#实参位置顺序很重要#默认值
def describe_pet(animal_type='dog',pet_name='penny'):
print("\nI have a "+animal_type+".")
print("My "+animal_type+"'s name is "+pet_name.title()+".")
describe_pet(pet_name='harry')
2479

被折叠的 条评论
为什么被折叠?



