1. 函数input()的工作原理

        函数input()让程序暂停运行,等待用户输入做一些文本。获取用户输入后,Python将其存储在一个变量中。例如下面的程序中,让用户输入一些文本,再将这些文本呈现给用户:

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

hello

        函数input()接受一个参数,这个参数可以给用户提示,让用户知道自己该怎么做。在这个示例中,Python运行第1行代码使用户看到提示信息,然后程序等待用户输入,并在用户按回车键后继续运行。用户的输入存储在变量message中,接下来的print(message)将输入呈现给用户。

2. 编写清晰的程序

        每当你使用函数input()时,都应该指定清晰而易于明白的提示,准确地指出你希望用户提供什么样的信息,如下所示:

name = input("Please enter your name: ")

print("Hello, " + name.title() + "!")

        输出结果为:

Please enter your name: aria

Hello, Aria!

        通过在提示信息结尾加上一个空格,可以将提示信息与用户输入分开,让用户清楚地知道自己的输入从哪开始。

        有时候,可能提示信息会超过一行。这种情况我们可以将提示信息放在一个变量中,再将该变量传递给函数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.title() + "!")

        输出结果为:

If you tell us who you are, we can personalize the messages you see.

What is your first name? aria



Hello, Aria!

        在此示例中演示了一种创建多行字符串的方式。第1行将消息的前半部分存储在变量prompt中,第2行中,运算符+=将prompt中的字符串末尾加上一个字符串,并存储在prompt中。最终的提示信息横跨两行,并在问号后面包含一个空格。

3. 使用int()来获取数值输入

        Python使用函数input()将用户输入解读为字符串。例如:

age = input("How old are you? ")

print(age)

print(age >= 18)

        输出结果为:

How old are you? 23

23

Traceback (most recent call last):

  File "e:\documents\learnPython\code\7_while_test.py", line 15, in <module>

    print(age >= 18)

TypeError: '>=' not supported between instances of 'str' and 'int'

        用户输入的是数字23,但是Python认为输入是字符串,并在数值比较时报错:'>=' not supported between instances of 'str' and 'int':不能将存储在变量age中的字符串’23’与数值18进行比较。也就是说,如果用户只是想打印输入,这样做是没有问题的,但是如果想要将输入作为数字使用就会引发错误。因此,我们需要将用户输入转换为数值再保存,此时需要用到函数int()。函数int()会将数字的字符串转换为数值表示。

age = input("How old are you? ")

age = int(age)#输入字符转化为int

print(age >= 18)

        输出结果为:

How old are you? 23

True

        在上述示例中,我们在提示信息后面输入23后,Python认为这个数值是字符串。第2行int()将这个字符串转换成了数值。最后将变量age(其中保存的是数值23)同数值18进行比较。输出结果为True。

4. 求模运算符

        处理数值信息时,求模运算符(%)是一个很有用的工具,它将两个数相除并返回余数:

>>> 4%3

1

>>> 5%3

2

>>> 6%3

0

>>> 7%3

1

        求模运算符不会返回两数相除之商,而是返回两数相除的余数。

        例如,判断一个数是奇数还是偶数,就可以利用求模运算符:

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: 11



The number 11 is odd.

        再次运行的结果:

Enter a number, and I'll tell you if it's even or odd: 12



The number 12 is even.

在上述示例中,利用偶数都能被2整除的特性,对输入的数值和2执行求模运算,返回结果为0则是偶数,否则是奇数。运行两次,第一次输入为11,返回结果是奇数,第二次输入为12,返回结果是偶数。

5. 在python2.7中获取输入

        如果你使用的Python是Python 2.7,那么应该使用raw_input()来提示用户输入。这个函数和Python 3中的input()一样,也将输入解读为字符串。

        Python 2.7也包含函数input(),但它将用户输入解读为Python代码,并尝试运行它们。因此可能出现意外的错误或者运行你不想运行的代码。因此如果你使用的是Python 2.7,请使用raw_input()而不是input()来获取输入。