1.用户输入函数 input() 的工作原理
函数 input() 让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中,以方便你使用。
>>> message = input("Tell me something, and I will repeat it back to you: ")
Tell me something, and I will repeat it back to you: hello Python!
>>> print(message)
hello Python!
当你使用 input() 时,应该指定清晰而易于明白的提示,准确地指出你希望用户提供什么样的信息。
2.使用 int() 来获取数值输入
使用函数 input() 时,Python将用户输入解读为字符串,所以一旦你想用用户输入的数据来跟其他类型比较时,就必须做类型转换。
>>> age = input("How old are you ?")
How old are you ?30
>>> age
'30'
如上所示我们得到了一个 30 的字符串。
>>> age >= 30
Traceback (most recent call last):
File "<pyshell#108>", line 1, in <module>
age >= 30
TypeError: '>=' not supported between instances of 'str' and 'int'
当我们做数值比较时报错。类型不匹配。所以这里就要用到 int() 函数将得到的字符串转换为 int 类型:
>>> int(age) >= 30
True
3.求模运算符(%)
用来处理数值信息,它能将两个数相除并返回余数:
>>> 4 % 3
1
>>> 5 % 3
2
>>> 6 % 3
0
>>> 7 % 3
1
可以用求模运算来判断一个数是奇数还是偶数。
4.while 循环
for 循环用于针对集合中的每个元素的一个代码块(循环次数根据指定集合中对象个数来判断),而 while 循环不断地运行,直到指定的条件不满足为止。编写 while 循环时注意循环条件,不要写了一个无限循环,哈哈。
语法:
while 循环条件:
执行逻辑
4.1.while 循环的退出
让用户自己选择退出:
>>> prompt = "
Tell me something, and I will repeat it back to you:"
>>> prompt += "
Enter '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 world!
hello world!
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program.quit
quit
使用标志:
>>> 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.hello world!
hello world!
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program.quit
使用 break 退出循环:
>>> prompt = "
Please enter the name of a city you have visited."
>>> prompt += "
(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() + "!")
Please enter the name of a city you have visited.
(Enter 'quit' when you are finished.)New York
I'd love to go to New York!
Please enter the name of a city you have visited.
(Enter 'quit' when you are finished.)Beijing
I'd love to go to Beijing!
Please enter the name of a city you have visited.
(Enter 'quit' when you are finished.)quit
4.2.循环中的 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
4.3.while 循环在 列表 和 字典 中的应用
循环列表,在循环列表时必须有个结束的逻辑,比如使用 pop() 函数或者 break 语句,否则进入无限循环。
>>> users = ['alice', 'brian', 'candace']
>>> while users:
print(users.pop())
candace
brian
alice
循环列表 users,依次移除列表 users 末尾的元素。直到最后一个元素被移除,循环结束。
>>> pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
>>> print(pets)
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
>>> while 'cat' in pets:
pets.remove('cat')
>>> print(pets)
['dog', 'dog', 'goldfish', 'rabbit']
循环列表 pets,循环移除元素 'cat' 直到 'cat' 全部移除 pets 中,循环结束。
循环字典
>>> responses = {}
>>> polling_active = True
>>> while polling_active:
name = input("
What is your name?")
reponse = input("Which mountain would you like to climb someday?")
responses[name] = reponse
repeat = input("Would you like to let another person respond?(yes/no)")
if repeat =='no':
polling_active = False
What is your name?dylan
Which mountain would you like to climb someday?Tai
Would you like to let another person respond?(yes/no)no
>>> for name, response in responses.items():
print(name + " would like to climb " + response + '.')
dylan would like to climb Tai.