int 对象在尝试对数字的数字求和时不可迭代?

发布于 2024-08-15 17:18:04 字数 228 浏览 4 评论 0原文

我有这段代码:

inp = int(input("Enter a number:"))

for i in inp:
    n = n + i;
    print (n)

但它抛出一个错误:'int' object is not iterable

我想通过添加每个数字来找出总数,例如 110.1 + 1 + 0 = 2。我该怎么做?

I have this code:

inp = int(input("Enter a number:"))

for i in inp:
    n = n + i;
    print (n)

but it throws an error: 'int' object is not iterable

I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?

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

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

发布评论

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

评论(11

像你 2024-08-22 17:18:04

首先,丢失对 int 的调用 - 您正在将字符串转换为整数,这不是您想要的(您希望将每个字符视为其自己的数字)。更改

inp = int(input("Enter a number:"))

为:

inp = input("Enter a number:")

现在 inp 是一串数字,您可以逐位循环它。

接下来,为 n 分配一些初始值 - 正如您现在的代码所示,您将得到一个 NameError 因为您从未初始化它。大概您希望在 for 循环之前使用 n = 0

接下来,再次考虑字符和整数之间的区别。现在你有了:

n = n + i;

除了不必要的分号(Python 是基于缩进的语法)之外,它还试图将字符 i 与整数 n 相加——这将不会'不工作!因此,this 就变成了

n = n + int(i)

将字符 '7' 转换为整数 7,依此类推。

First, lose that call to int - you're converting a string of characters to an integer, which isn't what you want (you want to treat each character as its own number). Change:

inp = int(input("Enter a number:"))

to:

inp = input("Enter a number:")

Now that inp is a string of digits, you can loop over it, digit by digit.

Next, assign some initial value to n -- as you code stands right now, you'll get a NameError since you never initialize it. Presumably you want n = 0 before the for loop.

Next, consider the difference between a character and an integer again. You now have:

n = n + i;

which, besides the unnecessary semicolon (Python is an indentation-based syntax), is trying to sum the character i to the integer n -- that won't work! So, this becomes

n = n + int(i)

to turn character '7' into integer 7, and so forth.

吲‖鸣 2024-08-22 17:18:04

也许您正在尝试

for i in range(inp)

这将打印您的输入值(inp)次,要仅打印一次,请执行以下操作:
对于范围内的 i(inp - inp + 1 )
print(i)

我刚刚遇到这个错误,因为我没有使用 range()

maybe you're trying to

for i in range(inp)

This will print your input value (inp) times, to print it only once, follow:
for i in range(inp - inp + 1 )
print(i)

I just had this error because I wasn't using range()

离笑几人歌 2024-08-22 17:18:04

try:

for i in str(inp):

这将迭代字符串表示形式中的字符。一旦你拥有了每个字符,你就可以像使用单独的数字一样使用它。

try:

for i in str(inp):

That will iterate over the characters in the string representation. Once you have each character you can use it like a separate number.

感情旳空白 2024-08-22 17:18:04

好吧,您想要处理表示数字的字符串,迭代数字,而不是数字本身(这是一个可以以不同方式编写的抽象实体,例如罗马数字中的“CX”或十六进制的“0x6e”(两者都代表 110)或其他)。

因此:

inp = input('Enter a number:')

n = 0
for digit in inp:
     n = n + int(digit)
     print(n)

请注意,n = 0 是必需的(在进入循环之前的某个位置)。您不能获取不存在的变量的值(并且 n = n + int(digit) 的右侧采用 n 的值) 。如果 n 确实存在,它可能包含与您当前需求完全无关的东西,从而导致意外的行为;你需要警惕这一点。

该解决方案不会尝试确保用户提供的输入实际上是数字。我将把这个问题留给你思考(提示:你需要的一切都在 Python 教程)。

Well, you want to process the string representing the number, iterating over the digits, not the number itself (which is an abstract entity that could be written differently, like "CX" in Roman numerals or "0x6e" hexadecimal (both for 110) or whatever).

Therefore:

inp = input('Enter a number:')

n = 0
for digit in inp:
     n = n + int(digit)
     print(n)

Note that the n = 0 is required (someplace before entry into the loop). You can't take the value of a variable which doesn't exist (and the right hand side of n = n + int(digit) takes the value of n). And if n does exist at that point, it might hold something completely unrelated to your present needs, leading to unexpected behaviour; you need to guard against that.

This solution makes no attempt to ensure that the input provided by the user is actually a number. I'll leave this problem for you to think about (hint: all that you need is there in the Python tutorial).

女中豪杰 2024-08-22 17:18:04

附注:如果你想获得所有数字的总和,你可以简单地做

print sum(int(digit) for digit in raw_input('Enter a number:'))

Side note: if you want to get the sum of all digits, you can simply do

print sum(int(digit) for digit in raw_input('Enter a number:'))
深海少女心 2024-08-22 17:18:04

for .. in 语句期望您使用定义了迭代器的类型。简单 int 类型没有迭代器。

for .. in statements expect you to use a type that has an iterator defined. A simple int type does not have an iterator.

惟欲睡 2024-08-22 17:18:04

正如 ghills 已经提到的那样,

inp = int(input("Enter a number:"))

n = 0
for i in str(inp):
    n = n + int(i);
    print n

当您循环遍历某些内容时,关键字是“IN”,请始终将其视为某些内容的列表。您不能循环遍历普通整数。因此,它是不可迭代的。

As ghills had already mentioned

inp = int(input("Enter a number:"))

n = 0
for i in str(inp):
    n = n + int(i);
    print n

When you are looping through something, keyword is "IN", just always think of it as a list of something. You cannot loop through a plain integer. Therefore, it is not iterable.

鹤仙姿 2024-08-22 17:18:04

获取您的输入并确保它是一个字符串,以便它是可迭代的。

然后执行列表理解并将每个值更改回数字。

现在,如果您愿意,您可以对所有数字求和:

inp = [int(i) for i in str(input("Enter a number:"))]
print sum(inp)

或者,如果您确实想在执行时查看输出:

def printadd(x,y):
    print x+y
    return x+y

inp = [int(i) for i in str(input("Enter a number:"))]
reduce(printadd,inp)

Take your input and make sure it's a string so that it's iterable.

Then perform a list comprehension and change each value back to a number.

Now, you can do the sum of all the numbers if you want:

inp = [int(i) for i in str(input("Enter a number:"))]
print sum(inp)

Or, if you really want to see the output while it's executing:

def printadd(x,y):
    print x+y
    return x+y

inp = [int(i) for i in str(input("Enter a number:"))]
reduce(printadd,inp)
天冷不及心凉 2024-08-22 17:18:04

OP-s问题的一个可能的答案(“我想通过添加每个数字来找出总数,例如,110。1 + 1 + 0 = 2。我该怎么做?”)是使用内置函数divmod()

num = int(input('Enter a number: ')
nums_sum = 0

while num:
    num, reminder = divmod(num, 10)
    nums_sum += reminder

One possible answer to OP-s question ("I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?") is to use built-in function divmod()

num = int(input('Enter a number: ')
nums_sum = 0

while num:
    num, reminder = divmod(num, 10)
    nums_sum += reminder
靑春怀旧 2024-08-22 17:18:04

你可以尝试改变
对于 inp 中的 i:
进入
对于范围内的 i(1,inp):
迭代不适用于单个 int。相反,您需要为其运行提供一个范围。

You can try to change
for i in inp:
into
for i in range(1,inp):
Iteration doesn't work with a single int. Instead, you need provide a range for it to run.

野鹿林 2024-08-22 17:18:04

不要将其设为 int(),而将其设为 range() 将解决此问题。

inp = range(input("Enter a number: "))

Don't make it a int(), but make it a range() will solve this problem.

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