1 input()输入函数
message = input("Tell me something, and I will repeat it back to you:");
print(message)
#由于sublime Text 不支持在打印界面输入输出,在终端运行命令
C:\Users\17315>D:\Users\17315\Desktop\python_work\7.py
Tell me something, and I will repeat it back to you:6
6
该函数令程序暂停,并输出一段自定义提示prompt,待用户输入任意元素后,函数将其赋值给一个自定义变量。
1)当提示语句太长时,可将提示语句赋值通过 自加符合 += 分段赋值给自定义变量,如下所示:
prompt = "If you tell us who you are, we can personalized the messages you see."
prompt += "\nWhat is your first name?\n"
name = input (prompt)
print(f"\nHello,{name}!")
output:
C:\Users\17315>D:\Users\17315\Desktop\python_work\7.py
If you tell us who you are, we can personalized the messages you see.
What is your first name?
li
Hello,li!
2)当希望输入值用于数值比较时,则需要用到int()函数
height = input("How tall you are, in inches?")
if int(height) >= 48:
print("\nYou are tall enough to ride!")
else:
print("You will be able to ride when you're a little older")
output:
C:\Users\17315>D:\Users\17315\Desktop\python_work\7.py
How tall you are, in inches?15
You will be able to ride when you're a little older
3)求模运算 运算符:%,即求余运算符号,只返回余数值,当被整除时返回0
number = input("Enter a number, and I will tell you if it's even or odd.\n")
if int(number) % 2 == 0:
print(f"The number:{int(number)} is even")
else:
print(f"The number:{int(number)} is odd")
output:
C:\Users\17315>D:\Users\17315\Desktop\python_work\7.py
Enter a number, and I will tell you if it's even or odd.
31
The number:31 is odd
2 While 循环 即 01判断,若为真,则返回1,否则返回0,python中可用True or False 表示
while current_number <= 5:
current_number += 1
print(current_number)
output:
C:\Users\17315>D:\Users\17315\Desktop\python_work\7.py
2
3
4
5
6
1) 通过定义“退出值”,当输入退出值时,循环结束
prompt = "\nTell me something, and I will repeat it back to you:\n"
prompt += "\nEnter 'quit' to end the program\n"
message = "" #赋予变量message 一个空字符,使while循环开始运行
while message != 'quit':
message = input(prompt)
if message !='quit':
print(message)
output:
C:\Users\17315>D:\Users\17315\Desktop\python_work\7.py
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program
nothing is better!
nothing is better!
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program
quit
2)通过使用标志变量使程序停止运行
prompt = "\nTell me something, and I will repeat it back to you:\n"
prompt += "\nEnter 'quit' to end the program\n"
active = True
while active:
message = input(prompt)
if message =="quit":
active = False
else:
print(message)
output:
C:\Users\17315>D:\Users\17315\Desktop\python_work\7.py
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program
sss
sss
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program
quit
3)使用break退出循环 退出后不再进行循环
prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\nPlease enter 'quit' when you are finished\n"
while True:
city = input(prompt)
if city != 'quit':
print(f"I'd love to go to {city.title()}")
else:
break
output:
C:\Users\17315>D:\Users\17315\Desktop\python_work\7.py
Please enter the name of a city you have visited:
Please enter 'quit' when you are finished
wuhan
I'd love to go to Wuhan
Please enter the name of a city you have visited:
Please enter 'quit' when you are finished
quit
4)continue 当满足某一条件测试的情况下,返回循环开头,重新测试
current_number = 1
while current_number < 20:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number)
output:
3
5
7
9
11
13
15
17
19
[Finished in 109ms]
提示:一定要在While循环中设置循环终止的条件,避免无限循环,一般按CTRL+C或关闭输出界面的方式结束无限循环
3 用While 循环处理列表和字典
1) 移动列表元素
unconfirmed_users = ['alice','brain','candace']
confirmed_users = []
while unconfirmed_users:
current_user = unconfirmed_users.pop()
print(f"Verifying user:{current_user.title()}")
confirmed_users.append(current_user)
confirmed_users.reverse() #将顺序调整一致
print(confirmed_users)
output:
Verifying user:Candace
Verifying user:Brain
Verifying user:Alice
['alice', 'brain', 'candace']
[Finished in 109ms]
2)删除为特定值的所有列表元素
pets = ['dog','cat','dog','cat','cat','pig','fox']
print(pets,"\n")
while 'cat' in pets: #自动查找列表中的'cat'
pets.remove('cat')
print(pets)
output:
['dog', 'cat', 'dog', 'cat', 'cat', 'pig', 'fox']
['dog', 'dog', 'pig', 'fox']
[Finished in 109ms]
3)使用用户来填充字典
responses = {}
active = True
while active:
name = input("What is your name?")
response = input("Which one is your favorite moutain?")
responses[name] = response #确立键值对关系
repeat = input("Would you like to let another person respond?(Yes or No)\n")
if repeat.title() == 'No':
active = False
print ("\t--- Poll Results ---\n")
for name,response in responses.items():
print(f"{name.title()} would you like climb {response}")
output:
C:\Users\17315>D:\Users\17315\Desktop\python_work\7.py
What is your name?li
Which one is your favorite moutain?huashan
Would you like to let another person respond?(Yes or No)
no
--- Poll Results ---
Li would you like climb huashan