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 :good
good
2. 编写清晰的程序
say_hellow = 'If you tell us who you are,we can presonalize the message you see.'
say_hellow += "\nWhat's your first name ?"
name = input(say_hellow)
print(f"Hellow,{name}!")
If you tell us who you are,we can presonalize the message you see.
What's your first name ?wang
Hellow,wang!
3.求模运算。处理数据信息,求模运算符(%)可以将两个数相除的余数返回。
print(4%3)
1
4. while 循环,会在判断条件不满足时一直运行。
current_num = 1
while current_num <= 6:
print(current_num)
current_num += 1
1
2
3
4
5
6
5. 使用标签。很多条件都满足才能继续运行的程序中,可以定义一个变量,用于判断整个程序是否处于活动状态。
这个变量成为标志(flag),充当程序的交通信号灯。
say_hellow = 'If you tell us who you are,we can presonalize the message you see.'
say_hellow += "\nWhat's your first name ?"
active = True
while active:
name = input(say_hellow)
if name == 'quit':
active = False
else:
print(f"Hellow,{name}!")
If you tell us who you are,we can presonalize the message you see.
What's your first name ?wang
Hellow,wang!
If you tell us who you are,we can presonalize the message you see.
What's your first name ?qiutHellow,qiut!
If you tell us who you are,we can presonalize the message you see.
What's your first name ?quit
6. 使用break退出循环
prompt = "\n Please enter the name of a city you have visited"
prompt += "\nEnter 'quit' when you finished."
while True:
city = input(prompt)
if city == 'quit':
break
else :
print(f"I'd love to go to {city.title()}")
Please enter the name of a city you have visited
Enter 'quit' when you finished.beijing
I'd love to go to BeijingPlease enter the name of a city you have visited
Enter 'quit' when you finished.quit
7. 使用continue跳出单次循环
要返回循环开头,并根据条件测试结果决定是否继续执行循环,使用continue语句。
一下代码实现11以内奇数的输出。
currrent_value = 0
while currrent_value <= 11: #大于11终止循环
currrent_value += 1
if currrent_value %2 ==0: #变量除2的余数为零,跳出本次循环返回循环开始
continue
print(currrent_value) #不打印余数为2(偶数)的值
1
3
5
7
9
11
8. 避免无限循环
while循环中判断循环终止的条件无法达到,则会无限执行下去,此时可以使用 Ctrl + C ,结束无限循环。
9. 使用while循环处理列表和字典
for循环是遍历列表的有效方式,但应避免在for循环中修改列表,否则python将难以跟踪其中的元素。
可以使用while循环来处理列表和字典。(以下代码实现 在列表之间移动元素 )
unconfirmed_users = ['alice','wang','zhao','li'] #创建一个待认证用户列表
confirmed_users = [] #创建一个空列表,用于存储已认证用户
while unconfirmed_users:
confirming_user = unconfirmed_users.pop() #弹出unconfirmed_users列表最后一个用户(元素),赋值至confirming_user
print(f"Verifying user : {confirming_user.title()}")
confirmed_users.append(confirming_user) #弹出的用户追加至空列表-confirmed_users
print("\nThe follow users have confirmed:")
for confirmed_user in confirmed_users: #遍历打印confirmed_users中的用户(元素)
print(confirmed_user.title())
Verifying user : Li
Verifying user : Zhao
Verifying user : Wang
Verifying user : Alice
The follow users have confirmed:
Li
Zhao
10. 删除列表中特定值所有元素
remove()可以删除列表中特定值(第一个),使用while循环删除所有特定值。
pets = ['dog','cat','dog','fish','tutle','cat','mouse','cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
['dog', 'cat', 'dog', 'fish', 'tutle', 'cat', 'mouse', 'cat']
['dog', 'dog', 'fish', 'tutle', 'mouse']
11. 使用用户输入充填字典
results = {} #创建空字典用于存储调查结果
polling_active = True #设置while语句的标识,用于控制是否终端循环
while polling_active:
name = input('\n请输入你的名字') #输入调查者的名字
polling_result = input("What's your favorite thing in weekend?") #输入调查结果
results[name] = polling_result #将调查结果存入字典
repeat = input("Anyone else?(yes or no)") #看看是否还有人参与调查
if repeat == 'no':
polling_active = False
print('\n~~~ poll results ~~~')
for name , result in results.items():
print(f"{name}‘s favorite thing is {result}!")
请输入你的名字>? wang
What's your favorite thing in weekend?>? eating
Anyone else?(yes or no)>? yes
请输入你的名字>? li
What's your favorite thing in weekend?>? reading
Anyone else?(yes or no)>? yes
请输入你的名字>? zhang
What's your favorite thing in weekend?>? running
Anyone else?(yes or no)>? yes
请输入你的名字>? zhao
What's your favorite thing in weekend?>? sleeping
Anyone else?(yes or no)>? nowang‘s favorite thing is eating! li‘s favorite thing is reading! zhang‘s favorite thing is running! zhao‘s favorite thing is sleeping!