#下面的例子让用户输入一些文本,再将这些文本呈现给用户
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: Hello everyone!
Hello everyone!
#下面是例子
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!
#下面是int()获取数值输入的例子
height = input("How tall are you, in inches? ")
height = int(height)
if height >= 36:
?? ?print("\nYou‘re tall enough to ride!")
else:
? ? print("\nyou‘ll be abke to ride when you‘re a little older.")
------------------------------------------------------------------
How tall are you, in inches? 71
You‘re tall enough to ride!
#可以利用这个判断一个数是奇数还是偶数,下面是例子
number = input("Enter a number, and I‘ll tell you if it‘s even or odd: ")
number = int(number)
if number % 2 == 0:
?? ?print("\nThe number " + str(number) + " is even.")
else:
?? ?print("\nThe number " + str(number) + " is odd.")
------------------------------------------------------------------
Enter a number, and I‘ll tell you if it‘s even or odd: 42
The number 42 is even.
#下面例子是从一数到五
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
#这里将quit也打印出来了,我们可以加一条条件测试
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)
------------------------------------------------------------------------------------
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
#下面是标志的例子
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter ‘quit‘ to end the program. "
active = True
#我们将变量active设置成了True,让程序最初处于活动状态。
while active:
?? ?message = input(prompt)
?? ?if message == ‘quit‘:
?? ??? ?active = False
?? ?else:
?? ??? ?print(message)
#我们使用了一个标志来指出程序是否处于活动状态,这样如果要添加测试
(如elif语句)以检查是否发生了其他导致active变为False的事件,将很容易。
#退出循环的例子
prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter ‘quit‘ when you are finished.) "
while True:? ?#以while True打头的循环将不断运行,直到遇到break语句。
?? ?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.) San Francisco
I‘d love to go to San Francisco!
Please enter the name of a city you have visited:
(Enter ‘quit‘ when you are finished.) quit
#在任何Python循环中都可使用break语句。例如,可使用break语句来退出遍历列表或字典的for循环。
Q:是否继续执行循环
A:使用continue语句
#下面是使用continue从1数到10.但只打印其中的奇数的例子
current_number = 0
while current_number < 10:
?? ?current_number += 1
?? ?if current_number % 2 == 0:
?? ??? ?continue
#if语句检查current_number与2的求模运算结果。如果结果为0(意味着current_number可被2整除),就执行continue语句,让Python忽略余下的代码,并返回到循环的开头。
?? ?print(current_number)
------------------------------------------------------------------
1
3
5
7
9
# 这个循环将没完没了地运行!
x = 1
while x <= 5:
?? ?print(x)
------------------------------------------------------------------
1
1
1
1
--snip--
#Ctrl + C可以退出循环,也可以关闭显示程序的终端窗口
原文:https://www.cnblogs.com/goodhelper007/p/User_input_and_while_loop.html