Python 基础之二用户交互input

时间:2022-08-20 21:22:19

Input是个内建函数:

 >>> input
<built-in function input>
>>>  

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

 >>> name = input("please input your name:")
please input your name:python
>>> name
'python'
>>> print(name)
python
>>>

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

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

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

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