Python中多个值的输入函数

发布于 2025-02-10 08:08:57 字数 180 浏览 1 评论 0原文

a,b=int(input("Enter two numbers").split())

print(a+b)

我已经编写了此代码以在使用输入()函数输入时添加两个数字。 它给出了一个错误。 我知道我们可以使用MAP功能转换为INT,但是此方法呢? 这里的错误是什么? [图片描述了它给出的错误]

a,b=int(input("Enter two numbers").split())

print(a+b)

I have written this code to add two numbers while taking input using input() function.
It is giving an error.
I know we can convert to int using map function but how about this method?
What is the error here?
[The pic depicts th error which it gives]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千寻… 2025-02-17 08:08:57

split返回字符串列表,因此您需要在该列表中的每个项目上调用int。在列表上调用int是没有意义的。例如

>>> a, b = [int(x) for x in "1 2".split()]
>>> a
1
>>> b
2
>>> 

split returns a list of strings, so you need to call int on each of the items in that list. It doesn't make sense to call int on a list. For example

>>> a, b = [int(x) for x in "1 2".split()]
>>> a
1
>>> b
2
>>> 
星軌x 2025-02-17 08:08:57

您可以尝试一下(使用 map 功能)。

a,b=map(int, input("Enter two numbers").split())

print(a+b)

You could try this (Using map function).

a,b=map(int, input("Enter two numbers").split())

print(a+b)
浮生未歇 2025-02-17 08:08:57
x, y = input("Enter two values: ").split()

print(int(x)+int(y))
x, y = input("Enter two values: ").split()

print(int(x)+int(y))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文