用户输入和while循环

  • Q:函数input()工作
  • A:函数让程序暂停运行,等待用户输入一些文本
# 下面的例子让用户输入一些文本,再将这些文本呈现给用户
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!

  • Q:函数input()的应用时的注意
  • A:应该指定清晰而易于明白的提示
# 下面是例子
prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "
What is your first name? "
name = input(prompt)
print("
Hello, " + name + "!")
------------------------------------------------------------------
If you tell us who you are, we can personalize the messages you see.
What is your first name? Eric

Hello, Eric!

  • Q:使用int()来获取数值输入
  • A:input()本身是获取字符串的
# 下面是int()获取数值输入的例子
height = input("How tall are you, in inches? ")
height = int(height)

if height >= 36:
    print("
You're tall enough to ride!")
else:
    print("
you'll be abke to ride when you're a little older.")
------------------------------------------------------------------
How tall are you, in inches? 71

You're tall enough to ride!

  • Q:求模运算符(%)
  • A:将两个数相除返回余数
# 可以利用这个判断一个数是奇数还是偶数,下面是例子
number = input("Enter a number, and I'll tell you if it's even or odd: ")
number = int(number)

if number % 2 == 0:
    print("
The number " + str(number) + " is even.")
else:
    print("
The 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.

  • Q:简单地使用while循环
  • A:不断的运行,直到指定的条件不满足为止
# 下面例子是从一数到五
current_number = 1

while current_number <= 5:
    print(current_number)
    current_number += 1
------------------------------------------------------------------
1
2
3
4
5

  • Q:让用户选择什么时候退出
  • A:定义一个退出值,只要用户输入的不是这个退出值,程序就接着运行
# 下面是选择什么时候退出的例子(粗体部分为用户输入)
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 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 = "
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)

    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
  • Q:要求很多条件都满足才能运行的程序怎么办?
  • A:使用标志  把相关逻辑由程序的其他部分处理
# 下面是标志的例子
prompt = "
Tell me something, and I will repeat it back to you:"
prompt += "
Enter '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的事件,将很容易。

  • Q:退出循环
  • A:break语句用于控制程序流程,可使用它来控制哪些代码行将执行,哪些代码行不执行
# 退出循环的例子
prompt = "
Please enter the name of a city you have visited:"
prompt += "
(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

  • Q:避免无限循环
  • A:对每个while条件测试
# 这个循环将没完没了地运行!
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