【发布时间】:2021-07-24 09:15:24
【问题描述】:
我尝试使用函数找到用户输入的两个数字中较大的数字。请帮我找出我在代码中犯的导致错误结果的错误:
def func1(n1,n2):
if (n1 > n2):
print(n1," is greater than ",n2)
else:
print(n2," is greater than ",n1)
print("Find which number is greater")
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
func1(num1, num2)
显示错误的结果:
Find which number is greater
Enter the first number: 10
Enter the second number: 5
5 is greater than 10
【问题讨论】:
-
input()总是返回一个字符串,您需要在比较之前将这些字符串转换为数字(int()) -
谢谢一百万,是的,我改成赞
num1 = int(input("Enter the first number: "))解决了。 -
输入函数产生一个字符串值,所以转换输入函数
-
就个人而言,我不喜欢 int(input...),我使用 Pydantic。看我的例子
标签: python python-3.x