1.工作原理:
函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其
int():类似于类型转换,将用户输入的字符串转换为int类型
3.求模运算
每个while循环都必须有停止运行的途径,务必对每个while循环进行测试,确保可以正常
退出while循环。
使用while循环来处理列表和字典
for循环是一种遍历列表的有效方式,但在for循环中不应修改列表,否则将导致python难以
跟踪其中的元素。要在遍历列表的同时对其进行修改,可用while循环。通过将while循环同列表
函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其
存储在变量中,以待使用。用户输入内容被解读为字符串。
message = input("Please input something: ") print(message) #每当使用input()时,应制定清晰而易于明白的提示,准确地指出你希望用户提供的信息 #通过在提示末尾包含一个空格,可将提示与用户输入分开,让用户清楚了解输入始于何处 #当提示超过一行时,可将提示存储在一个变量中,再将该变量传递给input()。 promt = input("If you tell us who you are ,we can personalize the message you see.") promt += "\nWhat is your first name? " name = input(promt) print("\nHello, " + name + "!") Output: If you tell us who you are ,we can personalize the message you see #等待用户输入 What is your first name? Shaw #输入为Shaw Hello, Shaw!2.获取数值输入
int():类似于类型转换,将用户输入的字符串转换为int类型
3.求模运算
%:将两个数相除,并返回余数。可用来判断一个数是不是偶数。
number = input("Enter a number,and I'll tell you if it is even or odd: ") number = int(number) if number % 2 == 0: print("\nThe number " + str(number) + " is even.") else : print("\nThe number " + str(number) + " is odd.")
4.while循环:直到制定的条件不满足为止
利用while让用户选择何时退出 promt = "\nTell me something, and i will repeat it back to you: " promt +="\nEnter 'quit' to end the program." promt +="\nNow you can enter what you want:\n " message = '' #初始值设定为空,进入while循环 while message != 'quit': message = input(promt) if message != 'quit': #不是quit指令时,打印出输入内容 print(message) #利用标志修改上述程序 active = True #设定初始标志为True while active: message = input(promt) if message == 'quit': active = False else: print(message) #利用break退出循环(在任何循环语句中都可以使用break退出循环) promt = "\nPlease enter the name of a city you have visited:" promt += "\n(Enter 'quit' when you are finished.)" while True: city = input(promt) if city == 'quit': break else: print("I'd love to go to " + city.title() + "!") #在循环中使用continue #打印1到10的奇数 current_number = 0 while current_number < 10: current_number += 1 if current_number % 2 == 0: continue print(current_number)避免while无限循环
每个while循环都必须有停止运行的途径,务必对每个while循环进行测试,确保可以正常
退出while循环。
使用while循环来处理列表和字典
for循环是一种遍历列表的有效方式,但在for循环中不应修改列表,否则将导致python难以
跟踪其中的元素。要在遍历列表的同时对其进行修改,可用while循环。通过将while循环同列表
和字典结合使用,可收集、存储并组织大量输入。
#情景:有一个列表,其中包含新注册但还未验证的网站用户;验证这些用户之后,将他们 #转移到另一个已验证用户列表。 #思路:使用while循环,在验证用户的同时将其从列表中提取出来,并加入另一个已验证 #用户列表中。 #首先,创建一个待验证用户表和一个已验证用户表 unconfirmed_users = ['alex','shaw','poison'] confirmed_users = [] #验证每个用户,直到没有未验证用户为止 #将每个经过验证的用户转移到已验证用户列表中 while unconfirmed_users: #直到表中所有元素被验证完结束 current_usr = unconfirmed_users.pop() print("Veryfying user: " + current_usr.title()) #显示所有已验证用户 print("\nThe following users have been confirmed: ") for confirmed_user in confirmed_users: print(confirmed_user.title()) #删除包含特定值的所有列表元素 pets = ['dog','cat','dog','cat','cat'] print(pets) whiel 'cat' in pets: #删除所有的'cat',直到表中没有'cat' 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 repond?(yes / no )") if repeat == 'no': polling_active = False #调查结束,显示结果 print("\n--------Poll Resaults--------") for name,response in responses.items(): print(name + " would like to climb " + response + ".") Output: What is your name?Alex Which mountain would you like to climb someday?Changjiang Would you like to let another person repond?(yes / no )yes What is your name?Shaw Which mountain would you like to climb someday?Hunghe Would you like to let another person repond?(yes / no )no --------Poll Resaults-------- Alex would like to climb Changjiang. Shaw would like to climb Hunghe.