我做了一个添加两个 nos.in python 的简单程序 我的代码是:
#python 3
def sum(x,y):
return x+y
a= int(input("Enter first number:"))
b= int(input("Enter second number:"))
print("Sum of the given two numbers is: ", sum(a,b))
输出错误如下:
失败案例 #1/8:无法检查答案。可能输出格式不对。
Input:
0 0
ValueError : invalid literal for int() with base 10: '0 0'
最佳答案
您的代码按预期运行良好,但您必须输入如下内容:
Enter first number:1
Enter second number:2
Sum of the given two numbers is: 3
而且您还创建了一个内置的 sum
方法,因此您可以:
a= int(input("Enter first number:"))
b= int(input("Enter second number:"))
print("Sum of the given two numbers is: ", sum(a,b))
如果没有 sum
方法,您创建的方法也可以工作,甚至更好地使用 +
作为两个值:
a= int(input("Enter first number:"))
b= int(input("Enter second number:"))
print("Sum of the given two numbers is: ", a+b)
关于python - 如何修复 2 位数总和的 python 代码中的 ValueError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53774720/