7.Python and user interaction

Python and user interaction

A. Why interactive?

Receiving a user input, control computer, meet our needs

II. How to interact?

#代码:
print('love '*5)
input("请输入你的姓名:")
#结果
love love love love love 
请输入你的姓名:45120

Now we are a handwritten landing system

#代码:
name = input("请输入你的姓名:")
pwd = input("请输入你的密码:")
print(type(name))
print(type(pwd))
#结果:
请输入你的姓名:520
请输入你的密码:520
<class 'str'>
<class 'str'>

Note: regardless of the value of our input is to receive the value of numeric types, strings, lists, input is of type string

age = input('请输入你的年龄:')         #age接受的是字符串类型
print(age,type(age))
age = int(age)                        #强制类型转换,但是int只能转换纯数字的字符串
print(age,type(age))
# 执行结果:
# 请输入你的年龄:11
# 11 <class 'str'>
# 11 <class 'int'>

Three .Python2 interaction

#代码:
name = raw_input("请输入你的姓名:")
pwd = raw_input("请输入你的密码:")
print(type(name))
print(type(pwd))

Note: Python2 in raw_input () and Python3 in input () is exactly the same

x= input('haha')  
#必须输入明确的数据类型,你输入什么类型,则接什么数据类型,输入'haha'而不是haha,否则会报错

Guess you like

Origin www.cnblogs.com/LWX-YEER/p/12421137.html