【问题标题】:Getting error message Traceback (most recent call last): File "input/code.py", line 1, in <module> a= int(input())获取错误消息 Traceback(最近一次调用最后):文件“input/code.py”,第 1 行,在 <module> a= int(input())
【发布时间】:2021-12-16 09:26:40
【问题描述】:

您好,这是一个简单的代码,它返回 a 和 b 的总和并要求用户输入。 这是针对竞争性编程网站的,例如代码部队,但不是代码部队网站。

我收到这个愚蠢的错误,我不知道为什么 Ther error

我的代码

a= int(input())
b= int(input())

sum= int(a)+ int(b)

print(sum)

谁能告诉我为什么会出现这个错误

【问题讨论】:

  • input() 返回整行。 a = int(input()) 尝试将字符串 3 2 转换为整数,导致您的错误

标签: python python-3.x error-handling


【解决方案1】:

正如我在评论中提到的,input() 返回整行。您必须用空格将其拆分以获得['3','2'],然后将其转换为整数。

一种方法是

line = input().split()
a,b = int(line[0]), int(line[1])

或者如果你喜欢一个班轮,

a,b = map(int, input().split())

【讨论】:

    猜你喜欢
    • 2018-10-11
    • 1970-01-01
    • 2021-04-10
    • 2022-10-13
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多