如何修复2位数字之和的python代码中的ValueError?

时间:2018-12-14 06:48:07

标签: python python-3.x

我做了一个简单的程序,在python中添加了两个nos。 我的代码是:

#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'

3 个答案:

答案 0 :(得分:4)

您的代码可以按预期工作,但是您必须输入以下内容:

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)

答案 1 :(得分:0)

在代码中,您在单独的行中输入了内容,而在代码中,您在一行中使用了逗号分隔的内容。 因此,请尝试在

等单独的行中进行输入
 0
 0

或像
一样更改输入脚本         a, b = map(int, input().strip().split())

这将解决您的问题。

答案 2 :(得分:0)

您没有提供int的{​​{1}}值。 请参阅python文档:https://docs.python.org/3/library/functions.html#input

base 10

下面是您在 class int([x]) class int(x, base=10) Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x defines __int__(), int(x) returns x.__int__(). If x defines __trunc__(), it returns x.__trunc__(). For floating point numbers, this truncates towards zero. If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2–36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int('010', 0) is not legal, while int('010') is, as well as int('010', 8). 中运行的程序。问题在于您的程序如何期望输入以及您实际如何提供输入。 您要求程序

a)在标准输出上打印“输入第一个数字”,然后将值分配给ipython,同时告诉程序它必须为'int'

然后

b)在标准输出上打印“输入第二个数字”,并将值分配给a 然后, 对分配给'a'和'b'的两个值求和,然后打印该总和

现在

看看第一次跑步。您正在完全按照编写程序的方式进行操作,提供了第一个值(按Enter键,如果输入正确,则标记移至下一步) 然后您提供第二个值,请输入Enter,然后得到结果

您提供的是int值,因此程序的工作原理就像一个魅力

b

Probelm在这里,您输入的第一个值是In [12]: 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)) ...: ...: Enter first number:0 Enter second number:0 Sum of the given two numbers is: 0 ,而不是0 0值。 int0不是 因此错误准确地说明了0 0的无效文字,表明它不是int。 int错误部分表明它期望使用小数(正常计数数字)

base 10

如果输入In [13]: 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)) ...: ...: Enter first number:0 0 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-13-4dd94dc4de5b> in <module>() 1 def sum(x,y): 2 return x+y ----> 3 a= int(input("Enter first number:")) 4 b= int(input("Enter second number:")) 5 ValueError: invalid literal for int() with base 10: '0 0' 而不是a

,您将得到相同的错误
int
相关问题