在python中查找最大/最小值的数字比较
我是python的初学者。我已经写了一个简单的程序来查找3个数字中最大的一个。当我输入相同位数的数字时,我得到了正确的答案,例如50 80 20。然而,当我给出输入50 130 20时,它不起作用 我做错了什么在python中查找最大/最小值的数字比较,python,python-3.x,Python,Python 3.x,我是python的初学者。我已经写了一个简单的程序来查找3个数字中最大的一个。当我输入相同位数的数字时,我得到了正确的答案,例如50 80 20。然而,当我给出输入50 130 20时,它不起作用 我做错了什么 num1=input("Enter 3 numbers\n") num2=input() num3=input() if(num1 > num2): if(num1 > num3): print("The greatest number is "+ s
num1=input("Enter 3 numbers\n")
num2=input()
num3=input()
if(num1 > num2):
if(num1 > num3):
print("The greatest number is "+ str(num1))
else:
print("the greatest number is "+ str(num3))
else:
if(num2 > num3):
print("The greatest number is " + str(num2))
else:
print("The greatest number is " + str(num3))
您可以使用 您是动态键入的另一个受害者 将数据读入num变量时,变量被视为字符串 当Python使用操作符比较两个字符串时,它是按字典顺序进行比较的,也就是按字母顺序进行比较。这里有几个例子
'apple' < 'orange' //true
'apple' < 'adam' //false
'6' < '7' //true as expected
'80' < '700' //returns false, as 8 > 7 lexiographically
如果你不想要TimTom的no max,这里有另一种方法:
num1= int("Enter 3 Number\n")
num2= int(input())
num3= int(input())
if num1 >= num2 and num1 >= num3:
numLarge = num1
# print exchange
elif num2 >= num1 and num2 >= num3:
numLarge = num2
# print exchange
else:
numLarge = num3
# print exchange
print("The greatest is " + str(numLarge))
这些是字符串而不是数字。使用intinput包装输入,字符串与数字的比较不同。是的,但我尝试在不使用max方法的情况下解决它。OP的问题不在于转换为完全正确的字符串,而在于不转换为int。您的代码将引发异常:无法添加字符串和数字。OP不会转换为字符串。函数输入已返回字符串。另一方面,在你的代码中,你试图添加最大的数字是一个字符串,而intnum1是一个数字,这是不可能的。完全搞砸了我如何修复他的代码。答案已编辑。谢谢你指出这一点!
num1=int("Enter 3 numbers\n")
num2=int(input())
num3=int(input())
if(num1 > num2):
if(num1 > num3):
print("The greatest number is "+ str(num1))
else:
print("the greatest number is "+ str(num3))
else:
if(num2 > num3):
print("The greatest number is " + str(num2))
else:
print("The greatest number is " + str(num3))
num1= int("Enter 3 Number\n")
num2= int(input())
num3= int(input())
if num1 >= num2 and num1 >= num3:
numLarge = num1
# print exchange
elif num2 >= num1 and num2 >= num3:
numLarge = num2
# print exchange
else:
numLarge = num3
# print exchange
print("The greatest is " + str(numLarge))