python学习之InPut（）和While循环知识讲解
2018-07-27 14:46:01      个评论    来源：mugong11的博客

python学习之InPut（）和While循环知识讲解

message = input("Tell me something, and I will repeat it back to you: ")
print(message)
name = input("Please enter your name: ")
print("Hello, " + name + "!")
# 使用函数input()时， Python将用户输入解读为字符串。
# 为解决这个问题，可使用函数int()，它让Python将输入视为数值。函数int()将数字的字符
# 串表示转换为数值表示，如下所示:
age = input("How old are you ")
age = int(age)
if age >= 18:
print('true')
# 处理数值信息时， 求模运算符（ %）是一个很有用的工具，它将两个数相除并返回余数：
print(4%3)
# 如果你使用的是Python 2.7，请使用raw_input()而不是input()来获取输入。
current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1
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)
# 要立即退出while循环，不再运行循环中余下的代码，也不管条件测试的结果如何，可使用
# 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
else:
print("I'd love to go to " + city.title() + "!")
# 要返回到循环开头，并根据条件测试结果决定是否继续执行循环，可使用continue语句，它
# # 不像break语句那样不再执行余下的代码并退出整个循环。
current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number)
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 + ".")