首页 > 代码库 > Python 基础之二用户交互input

Python 基础之二用户交互input

Input是个内建函数:

>>> input

<built-in function input>

>>>

?

具体用法:接收用户输入的内容,输入的字符串,接收到就是字符串;输入的是数字,接收的就是数字

>>> name = input("please input your name:")

please input your name:like

>>> name

‘like‘

>>> print(name)

like

>>>

?

eval用法:可以接收之前定义的变量,结合input使用

>>> eval(‘name‘)

‘lk‘

>>> eval(input("please input your name:"))

please input your name:name

‘lk‘

?

如果输入的是之前未定义的变量,将报错:

>>> eval(input("please input your name:"))

please input your name:lk

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "<string>", line 1, in <module>

NameError: name ‘like‘ is not defined

>>> a = 5

>>> eval(input("please input your name:"))

please input your name: a

5

Python 基础之二用户交互input