python中eval()函数的一些使用方法

    xiaoxiao2023-11-21  148

    1、在使用eval(x)函数之后,对于输入的x变量若是多个,需要用“,”隔开,如

    def main(): x1,x2,x3=eval(input("Please enter three values: ")) print("The largest value is ",max(x1,x2,x3)) main()

    对于需要输入的三个数值(以5,6,7为例),一定要在控制台输入时使用“,”隔开,若是使用其他符号(如空格)就会出现语法错误,如下

    Please enter three values: 5 6 7 Traceback (most recent call last): File "D:/Program Files/Python37/练习文档/使用max函数求最大值.py", line 5, in <module> main() File "D:/Program Files/Python37/练习文档/使用max函数求最大值.py", line 2, in main x1,x2,x3=eval(input("Please enter three values: ")) File "<string>", line 1 5 6 7 ^ SyntaxError: invalid syntax

    同样,也不能使用汉字的“,”,而是英文的“,”,否则也会报错,如下

    Please enter three values: 5,6,7 Traceback (most recent call last): File "D:/Program Files/Python37/练习文档/使用max函数求最大值.py", line 5, in <module> main() File "D:/Program Files/Python37/练习文档/使用max函数求最大值.py", line 2, in main x1,x2,x3=eval(input("Please enter three values: ")) File "<string>", line 1 5,6,7 ^ SyntaxError: invalid character in identifier

    最后是成功的结果

    Please enter three values: 5,6,7 The largest value is 7

     

    最新回复(0)