![](/img/trans.png)
[英]I got this error ValueError: not enough values to unpack (expected 2, got 1)
[英]ValueError: not enough values to unpack (expected 2, got 1) please help me with this error
T = int(input())
for tc in range(T):
# Read integers a and b.
(a, b) = map(int, input().split(' '))
ans = a + b
print(ans)
在这一行中: map(int, input().split(' '))
你应该输入两个值,但是当你输入one
值时你得到一个错误。 为了防止用户输入three or more
,你可以试试这个:
T = int(input())
for tc in range(T):
(a, b) = (int(input()) for _ in range(2))
ans = a + b
print(ans)
T = int(input("Enter number of times for loop to run as per your input"))
for tc in range(T):
(a, b) = map(int, input().split(' '))
ans = a + b
print(ans)
当你第一次运行时,它会询问你想要的测试用例的数量。 之后,您必须为您的数字提供两个数字之间的空格,正如您在split(' ')
中提到的那样。 如果您在split
中提到'-'
,则必须在两个输入数字之间提供'-'
。 此外,您可以在split()
之前添加strip()
函数,以避免由于输入中的空格而导致的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.