函数input() 的工作原理
message = input("Tell me something, and I will repeat it back to you: ")
//intput 显示字符串里面的值,然后让你输入一个值
print(message)
Tell me something, and I will repeat it back to you: Hello everyone!
Hello everyone!
提示可能超过一行, 例如, 你可能需要指出获取特定输入的原因。 在这种情况下, 可将提示存储在一个变量中, 再将该变量传递给函数input() 。 这样, 即便提示超过一行, input() 语句也非常清晰。
prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "\nWhat is your first name? "
name = input(prompt)
print("\nHello, " + name + "!")
If you tell us who you are, we can personalize the messages you see.
What is your first name? Eric
Hello, Eric!
>>> age = input("How old are you? ")//输入21 ,python 解读成字符串
How old are you? 21
❶ >>> age = int(age)//类型转换
>>> age >= 18
True
处理数值信息时, 求模运算符 (%) 是一个很有用的工具, 它将两个数相除并返回余数
>>> 4 % 3
1>
>> 5 % 3
2>
>> 6 % 3
0>
>> 7 % 3
1
while 循环简介
current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1
1
2
3
4
5
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)
print(message)
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. Hello everyone!
Hello everyone!
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. Hello again.
Hello again.
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. quit
quit
使用标志
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
❶ active = True//此时active 为标志
❷ 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//遇到break 跳出while
else:
print("I'd love to go to " + city.title() + "!")
注意 在任何Python循环中都可使用break 语句。 例如, 可使用break 语句来退出遍历列表或字典的for 循环。
在循环中使用continue
current_number = 0
while current_number < 10:
❶ current_number += 1
if current_number % 2 == 0:
continue//如果if true ,则直接返回 while 的判断语句
print(current_number)
删除包含特定值的所有列表元素
我们使用函数remove() 来删除列表中的特定值, 这之所以可行, 是因为要删除的值在列表中只出现了一次
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']
使用用户输入来填充字典
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 + ".")
What is your name? Eric
Which mountain would you like to climb someday? Denali
Would you like to let another person respond? (yes/ no) yes
What is your name? Lynn
Which mountain would you like to climb someday? Devil's Thumb
Would you like to let another person respond? (yes/ no) no
--- Poll Results ---
Lynn would like to climb Devil's Thumb.
Eric would like to climb Denali.