Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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
Python 从3个数字中找出最大的数字_Python_Python 3.x - Fatal编程技术网

Python 从3个数字中找出最大的数字

Python 从3个数字中找出最大的数字,python,python-3.x,Python,Python 3.x,我正在尝试使用python3.6查找3个数字中最大的一个。但它返回的值是错误的 #!/usr/bin/python3 a=input("Enter first number: ") b=input("Enter second number: ") c=input("Enter Third number: ") if (a >= b) and (a >= c): largest=a elif (b >= a) and (b >= c): largest=b

我正在尝试使用python3.6查找3个数字中最大的一个。但它返回的值是错误的

#!/usr/bin/python3

a=input("Enter first number: ")
b=input("Enter second number: ")
c=input("Enter Third number: ")
if (a >= b) and (a >= c):
    largest=a
elif (b >= a) and (b >= c):
    largest=b
else:
    largest=c

print(largest, "is the greatest of all")
如果我提供,a=15;b=10,c=9 预期产出应为15

但是我得到的实际输出是9。

您可以使用python的max()内置函数:

就像前面提到的注释一样,
您是在比较字符串,而不是数字赫尔伍德
。您应该首先将输入值转换为整数(或浮点),然后比较它们

有关python中强制转换的更多信息,请参见

input()
返回字符串。您需要将字符串转换为class int以比较其数值:

a = int(input("Enter the first number: "))
string
比较中,
9
大于
1
。您可以在REPL中尝试:

>>> '9' > '111111111111111111'
True

正如khelwood在评论中指出的,这些输入被用作字符串

请尝试在输入后插入此项并将其修复:

a = int(a)
b = int(b)
c = int(c)

您正在比较字符串,而不是数字。如果你将它们转换成数字,它们将作为数字进行比较。可能重复前面提到的as@khelwood将其转换为int->int(a)