python - 如何修复 2 位数总和的 python 代码中的 ValueError?

标签 python python-3.x

我做了一个添加两个 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/

相关文章:

Python正则表达式排除以字符串结尾

python - 为什么使用巴特沃斯滤波器进行低频滤波时会出现错误?

python - 如何使用 Python/pandas 获取带有行摘要的分钟/小时财务数据?

python - 如何过滤列表中的元素并生成频率表

python - 产生二叉树的所有根到叶分支

python - OpenCV属性错误: 'module' object has no attribute 'xfeatures2d'

python - 夹层部署 Apache2 wsgi 时遇到错误

python-3.x - 'for' 循环中未定义“var”

python - 格式化 csv 数据并将每一行写入 json

python - PBKDF2 中的盐 - Python