Getting to interact with the user's day3-01 python

First, the procedures and user interaction

1.1 What is the interaction with the user

User interaction is the person to the computer input / input data, the computer print / output

1.2 Why should interact with the user

In order to let the computer user like people and exchange

For example, when you go to the ATM machine withdrawals, ATM machines that require prompt you to enter a user name and password, if the password is correct, then tells you log in successfully, if the password is wrong, then tell you to fail.

1.3 How to interact with the user

The process is essentially the input and output of interaction

# 输入
name = input('请输入你的用户名:')  # 请输入你的登录名
age = input('请输入你的年龄:')  # 请输入你的年龄

# 输出
print(name)  # 输出用户名
print(age)  # 输出年龄

1.4 python2 and python3 difference in input of

input in 1.4.1 python3 ()

In python3the input receiving user input, no matter what the user's input, the final string must be returned

name = input('请输入你的用户名:')
age = input('请输入你的年龄:')

print(name)
print(type(name))  # 打印name的类型
print(age)
print(type(age))  # 打印age的类型

请输入你的用户名:kody
请输入你的年龄:21
kody
<class 'str'>  # 姓名返回的是字符串
21
<class 'str'>  # 年龄返回的是字符串

Process finished with exit code 0

1.4.2 python2 in input ()

In python2type, we use the input function must be declared to be entered

>>> name = input("请输入你的姓名:")
请输入你的姓名:sean  # 直接输入姓名,发现下面报错
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "<string>", line 1, in <module>
NameError: name 'sean' is not defined
>>> input("请输入你的姓名:")
请输入你的姓名:"sean"  # 在输入的时候加上双引号,声明输入内容为字符串
'sean'  # 没有报错,正常输出,返回的结果为字符串

>>> input(">>:")
>>:1  # 直接输入数字1
1  # 返回结果为数字类型

>>> input(">>:")
>>:[1,2]  # 输入列表
[1, 2]  # 返回结果为列表

In python2the raw_inputwith python3the input()same effect, the result is a string

>>> raw_input(">>:")
>>:sean  # 输入sean
'sean'  # 返回结果为字符串

>>> raw_input(">>:")
>>:12  # 输入数字
'12'  # 返回结果为字符串

1.4.3 summary

python2 in raw_inputthe python3 the inputsame effect

Guess you like

Origin www.cnblogs.com/cnhyk/p/11782606.html