原文原址https://blog.csdn.net/Cengineering/article/details/78491552
函数input()的工作原理
函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中,以方便使用。prompt="if you tell us who you are , we can personlize the message you see."
prompt+="\nWhat is your first name?"
name=input(prompt)
print("\nHello, "+name+"!")
if you tell us who you are , we can personlize the message you see.
What is your first name?Eric
Hello, Eric!
这个示例演示了一种创建多行字符串的方式。第1行将消息的前半部分存储在变量prompt中;在第2行中,运算符+=在存储在prompt中的字符串末尾附加一个字符串。
笔记
name=input(prompt)
input是请求用户输入;
(prompt)是提示用户输入的内容,正常是字符串,其它属性需要另外添加。
name用户输入的值会赋予在这里
While循环简介
for循环用于针对集合中的每一个元素都一个代码块,而while循环不断的运行,直到指定的条件不满足为止。
使用while循环
使用while循环current_number=1
while current_number<=5:
print(current_number)
current_number+=1
1
2
3
4
5
在第一行,我们将current_number设置为1,从而指定从1开始数。接下来的while循环被设置成这样:只要current_number小雨或等于5,就接着运行这个循环。循环中代码打印current_number的值,在使用current_number+=1(current_number=current_number+1)将其值加1。
只要满足条件current_number<=5,python就接着运行这个程序。
笔记
while循环可以理解为当什么什么的时候就怎么怎么样
具体:
current_number=1 给current_number赋值为1
while current_number<=5: 当 current_number<=5时
print(current_number) 打印 current_number
current_number+=1 打印完后 给current_number加上一 再加入循环 直到不满足current_number<=5
让用户选择何时退出
可用while循环让程序在用户愿意时不断地运行。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)
Tell me something,and I will repeat it back to you:
Enter 'quit' to end the program.hi
hi
Tell me something,and I will repeat it back to you:
Enter 'quit' to end the program.quit
我们定义了一条提示消息,告诉用户他有两个选择:要么输入一条消息,要么输入退出值。接下来,我们创建了一个变量——message,用于存储用户输入的值。我们将变量message的初始值设置为空字符串“”,让Python首次执行while代码行时有可供检查的东西。Python首次执行while语句时,需要将message的值与quit进行比较,但此时用户还没有输入。如果没有可供比较的东西,Python将无法运行程序。为解决这个问题,我们必须给变量message指定一个初始值。虽然这个初始值只是一个空字符串,但符合要求,让Python能够执行循环while所需的东西。只要循环不是‘quit’,这个循环就会不断运行。
笔记
【prompt="\nTell me something,and I will repeat it back to you:"
prompt+="\nEnter 'quit' to end the program."】给prompt赋值。
message="" 给message赋一个空值
while message !='quit': 当message不等于字符串quit时
message=input(prompt) 请求用户输入一个值 提示语是prompt 输入的值会赋到message上
if message !='quit': 如果此时message不等于字符串quit
print(message) 便打印message 并开始下一轮循环 直到message ='quit':
使用标志
在前一个示例中,我们让程序在满足指定条件时就执行特定的任务。但在更复杂的程序中,很多不同的事件都会导致程序停止运行;在这种情况下,该怎么办呢?
在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态。这个变量被称为标志,充当了程序的交通信号灯。你可让程序在标志为true时继续运行,并在任何事件导致标志的值为False时让程序停止运行。这样,在while语句中,就只需检查一个条件——标志的当前值是否为true,并将所有测试都放在其它地方,从而让程序变得更简洁。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)
Tell me something,and I will repeat it back to you:
Enter 'quit' to end the program.yes
yes
Tell me something,and I will repeat it back to you:
Enter 'quit' to end the program.quit
我们将变量设置为true,让程序处于活跃状态。这样简化了while语句,因为不需要在其中做任何比较——相关的逻辑由程序的其他部分处理。只要变量active为true,循环就继续这样运行。
在while循环中,我们在用户输入后使用一条if语句来检查变量message的值。如果用户输入的是‘quit’,我们就将变量active设置为False,这将导致while循环不再继续下去。如果用户输入的不是‘quit’,我们就输入作为一条消息打印出来。
笔记
【prompt="\nTell me something,and I will repeat it back to you:"
prompt+="\nEnter 'quit' to end the program."】给prompt赋值
active=True 给active赋值True
【while active: 当active为true时
message=input(prompt)】请求用户输入一个值赋予message 提示语为prompt
if message=='quit': 如果message等于quit
active=False 就给active赋值false
else: 否则
print(message) 打印message 加入下一场循环直到active为false
使用break退出循环
要立即退出循环,不再运行循环中余下的代码,也不管测试条件如何,可是用break语句。prompt="\nTell me something,and I will repeat it back to you:"
prompt+="\nEnter 'quit' to end the program."
while True:
message=input(prompt)
if message=='quit':
break
else:
print(message)
笔记
【prompt="\nTell me something,and I will repeat it back to you:"
prompt+="\nEnter 'quit' to end the program."】给prompt赋值
while True:当为真
message=input(prompt) 提示为message赋值
if message=='quit': 如果message的值为quit
break强制停止
else: 否则
print(message) 打印message 加入下一轮循环
在循环中使用continue
要返回到循环开头,并根据测试条件测试结果决定是否继续执行循环,可使用continue语句,它不像break语句那样不在执行余下的代码并退出整个循环。current_number=0
while current_number<10:
current_number+=1
if current_number %2==0:
continue
print(current_number)
1
3
5
7
9
我们首先将current_number设置成了0,由于它小于10,Python进入while循环。进入循环后,以步长为1的方式往上数,因此current_number为1。接下来,if语句检查current_number与2的求模运算结果。结果为0,就执行continue语句,让Python忽略余下的代码,并返回循环的开头。如果当前的数字不能被2整除,就执行循环中余下的代码,Python将这个数字打印出来。
笔记
current_number=0 给 current_number赋值为0
while current_number<10: 当current_number<10时
current_number+=1 current_number加1
if current_number %2=0: 如果current_number除以2之后的余数为0
continue 执行continue语句Python忽略余下的代码,并返回循环的开头
print(current_number) 不成立打印current_number·
在列表之间移动元素
假设有一个列表,其中包含新注册但还未验证的网站用户;验证这些用户后,如何将他们移动到另一个已验证的用户列表中呢?一种办法是使用一个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())
Verifying user: Candace
Verifying user: Brian
Verifying user: Alice
The following users have been confirmed:
Candace
Brian
Alice
我们首先创建了一个未验证用户列表,其中包含Alice、Brain、和Candace,还创建了一个空列表,用于存储以验证的用户。While循环不断的运行,直到列表unconfirmed_users变成空的。在这个循环中,函数pop()以每次一个的方式从列表unconfirmed_user末尾删除未验证的用户。由于Candace位于列表unconfirmed_users末尾,因此名字将首先被删除、存储到变量current_user中并加入到列表confirmed_users中。
为模拟用户验证过程,我们打印一条验证消息并将用户加入到已验证用户列表中。未验证用户列表越来越短,已验证用户列表越来越长。未验证列表为空后结束循环,再打印已验证列表。
笔记
# 首先,创建一个待验证用户列表
#和一个用于存储已验证用户的空列表
unconfirmed_users=['alice','brian','candace'] 赋值
confirmed_users=[] 赋值
#验证每个用户,知道没有未验证用户为止
#将每个经过验证的列表都移到已验证用户列表中
while unconfirmed_users: 当未认证用户可以被随机枪毙时
current_user=unconfirmed_users.pop() current_user会是那个枪毙名单上的人
【 print("Verifying user: "+current_user.title()) 打印 current_user的开头大写形态 前面加上"Verifying user: "】
confirmed_users.append(current_user) confirmed_users末尾添加上current_user的值
#显示所有已验证的用户
【print("\nThe following users have been confirmed:") 空一行打印The following users have been confirmed:】
for confirmed_user in confirmed_users: 一个一个的从confirmed_users中提取 confirmed_user
print(confirmed_user.title()) 然后以开头字母大写的方式一个一个的打印出
Verifying user: Candace
Verifying user: Brian
Verifying user: Alice
The following users have been confirmed:
Candace
Brian
Alice
这里while循环和 print for循环是分开的,相互独立,看空格区分。
删除包含特定值的所有列表元素
前面我们使用remove()来删除列表中的特定元素,这之所以可行,是因为要删除的值在列表中只出现了一次。如果要删除列表中所有包含特定值的元素,该怎么办呢?
假设你有一个宠物列表,其中包含多个值为‘cat’的元素。要删除所有这些元素,可不断运行一个while循环,直到列表中不再包含值‘cat’.pets=['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'rabbit']
笔记
pets=['dog','cat','dog','goldfish','cat','rabbit','cat'] 建立一个列表
print(pets) 打印列表
while 'cat' in pets: 当字符串cut在pets里面的时候
pets.remove('cat') 就移走pets里的cat
print(pets) 打印pets
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat'] 这是刚建立起的pets
['dog', 'dog', 'goldfish', 'rabbit'] 这是移走cut的pets
使用用户输入来填充字典
可使用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)")
polling_active=False
#调查结束,显示结果
print("\n---Poll Results---")
for name,response in responses.items():
print(name+" would like to climb "+response+".")
What is your name?hi
which mountain would you like to climb someday?ko
Would you like to let another person respond?(yes/no)yes
What is your name?ji
which mountain would you like to climb someday?kp
Would you like to let another person respond?(yes/no)no
---Poll Results---
hi would like to climb ko.
ji would like to climb kp.
这个程序首先定义了一个空字典,并设置了一个标志,用于指出调查是否继续。只要polling_active为True,Python就运行while循环中的代码。
在这个循环中,提示用户输入其用户名及其爬那座山。将这些信息存储在字典response中,然后询问用户调查是否继续。如果用户输入yes,程序将再次进入while循环;如果用户输入no,标志polling_active将被设置为False,而while循环就此结束。
笔记
responses={} 建立一个空的集合
#设置一个标志,指出调查是否继续
polling_active=True
while polling_active: 当polling_active是true的状态的时候
#提示输入被调查者的名字和回答
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)")
polling_active=False
#调查结束,显示结果
print("\n---Poll Results---")
for name,response in responses.items():
print(name+" would like to climb "+response+".")
What is your name?hi
which mountain would you like to climb someday?ko
Would you like to let another person respond?(yes/no)yes
What is your name?ji
which mountain would you like to climb someday?kp
Would you like to let another person respond?(yes/no)no
---Poll Results---
hi would like to climb ko.
ji would like to climb kp.