1、函数input()的工作原理:
-
函数input()让程序暂停运行,等待用户输入一些文本。
message = input("Tell me something,and I will repeat it back to you:") print(message) Tell me something,and I will repeat it back to you:Helo everyone! Hello everyone!
-
使用int()来获取数值输入:
使用函数input()时,Python将用户输入解读为字符串。
函数 int()将数字的字符串表示为数值表示。
age = input("How old are you?") How old are you? 21 age = int(age) age >= 18 True
-
求模运算符:
处理数值信息时,求模运算符(%)是一个很有用的工具,他将两个数相除并返回余数。
number = input("Enter a number, and I'll tell you if it's 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.")
-
python 2.7中使用函数raw_input()来提示用户输入。
2、while循环:
-
让用户选择何时退出:
prompt = "\nTell me so mething, 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) Tell me so mething, and I will repeat it back to you: Enter 'quit' to end the program.fgdaskh fgdaskh Tell me so mething, and I will repeat it back to you: Enter 'quit' to end the program.quit
-
使用标志:
在要求很多条件都满足才继续运行的程序中,可定义一个变量;用于判断整个程序是否处于活动状态。(这个变量被称为标志,充当程序的交通信号灯。)
prompt = "\nTell me so mething, and I will repeat it back to you:" prompt += "\nEnter 'quit' to end the program." #active为标志。 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
#打印0~9之间的偶数 current_number = 0 while current_number < 10: current_number += 1 if current_number % 2 ==0: continue print(current_number)
-
如果程序陷入无限循环,可按Ctrl + C,也可关闭显示程序输出的终端窗口。
3、使用while循环来处理列表和字典:
-
在列表之间移动元素:
#首先,创建一个待验证用户列表 #和一个存储已验证用户的空列表 unconfirmed_users = ['aice','brian','candace'] confirmed_users = [] #验证每个用户,知道没有未验证用户为止 #将每个经过验证的列表都移到已验证用户列表中 while unconfirmed_users: current_user = unconfirmeed_users.pop() print("Verifying us er: " + current_user.title()) confirmed_users.append(current_user) #显示所有已验证的用户 print("\nThe following user have been confirmed:") for confirmed_user in confirmed_user
2.删除包含特定值的所有列表元素:
- 使用while删除列表中的多个重复值:
#宠物列表
pets = ['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
whie 'cat' in pets:
pets.remove('cat')
print(pets)
#运行结果
['dog','cat','dog','goldfish','cat','rabbit','cat']
['dog','dog','goldfish','rabbit']
3.使用用户输入来填充字典:
- 使用while循环提示用户输入任意数量的信息:
#创建一个调查程序
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 +".")