Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么max()在python中产生错误的输出?_Python_Max - Fatal编程技术网

为什么max()在python中产生错误的输出?

为什么max()在python中产生错误的输出?,python,max,Python,Max,输入为45295440-214542-641-356-6 输出应该是542,但我得到的输出是6 将它们转换为整数,然后应用函数 numbers=input("Enter numbers separated by space") x=numbers.split() mx=max(x) print(mx) 输出: numbers=input("Enter numbers separated by space") x=[int(i) for i in numbers.split()] mx=max(

输入为45295440-214542-641-356-6


输出应该是542,但我得到的输出是6

将它们转换为整数,然后应用函数

numbers=input("Enter numbers separated by space")
x=numbers.split()
mx=max(x)
print(mx)
输出:

numbers=input("Enter numbers separated by space")
x=[int(i) for i in numbers.split()]
mx=max(x)
print(mx)

输入是字符串。您需要将其转换为整数

542

其他人已经解释了发生了什么,但由于Python有一个repl,我将用它来向您展示Python认为正在发生的事情

numbers=input("Enter numbers separated by space")
x=map(int, numbers.split())
mx=max(x)
print(mx)

#542
把它们放在一起

>>> [int(x) for x in nums.split(' ')]
[1, 2, 3, 4, 5]
为了进一步解释上述内容

>>> max([int(x) for x in nums.split(' ')])
5
>>'1'==1
假的
>>> '1' < 2
回溯(最近一次呼叫最后一次):
文件“”,第1行,在

TypeError:'字符串按字典顺序进行比较。将字符串转换为整数。但是,6如何大于542,或者如何使用它们的整数形式作为max@RakshithKrish字符串“6”大于字符串“542”,因为它以更大的字符开头。这与词典中“fog”在“epicycle”之后出现的原因相同,尽管后者有更多的字符。
>>> [int(x) for x in nums.split(' ')]
[1, 2, 3, 4, 5]
>>> max([int(x) for x in nums.split(' ')])
5
>>> '1' == 1
False
>>> '1' < 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  TypeError: '<' not supported between instances of 'str' and 'int'