繁体   English   中英

ValueError:没有足够的值来解压(预期 2,得到 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM