python上的输入函数和eval函数有什么区别

时间:2019-08-07 16:22:46

标签: python python-3.5

我没有o图片,但我问了一个问题,因为我是使用python的初学者

2 个答案:

答案 0 :(得分:2)

input()将用户输入作为字符串。非常安全。

>>> usr = input('Enter some input: ')
Enter some input: hello, world
>>> usr
"hello, world"

eval()将像执行python代码一样执行字符串。非常危险。

>>>eval(input('Make it happen!'))
Make it happen! print('hello')
hello
>>>eval(input('Make it happen!'))
Make it happen! os.system('echo malicious things')

现在您真的把计算机弄糟了。

答案 1 :(得分:1)

eval()用于评估表达式,input()用于接受用户输入。以下是示例:

#evaluates expression
>> eval('5+2')
>> 7
# Takes user input
>> input()
10 (user enters)
>> 10

#evaluates user input
>> eval('input()')
15 (user enters)
>> 15
相关问题