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

时间:2019-05-09 08:22:49

标签: 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
else:
    largest=c

print(largest, "is the greatest of all")

如果我提供的话,a = 15; b = 10和c = 9 预期输出应为15。

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

4 个答案:

答案 0 :(得分:1)

您可以使用python的max()内置函数: https://www.freecodecamp.org/forum/t/python-max-function/19205

答案 1 :(得分:1)

input()返回字符串。您需要将字符串转换为int类以比较它们的数值:

a = int(input("Enter the first number: "))

string比较中,9大于1。您可以尝试REPL:

>>> '9' > '111111111111111111'
True

答案 2 :(得分:0)

khelwood的评论提到它You are comparing strings, not numbers. – khelwood。您应该首先将输入值转换为整数(或浮点数),然后进行比较。

可以在here

中找到有关python转换的更多信息

答案 3 :(得分:0)

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

尝试在输入后将其插入并固定:

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