TypeError: 'int'对象不支持项目赋值。

时间:2022-08-27 00:32:29

Why do I get this error?

为什么会出现这个错误?

    a[k] = q % b
 TypeError: 'int' object does not support item assignment

Code:

代码:

def algorithmone(n,b,a):
     assert(b > 1)
     q = n
     k = 0
     while q != 0:
        a[k] = q % b
        q = q / b
        ++k

     return k

print (algorithmone(5,233,676))
print (algorithmone(11,233,676))
print (algorithmone(3,1001,94))
print (algorithmone(111,1201,121))

1 个解决方案

#1


15  

You're passing an integer to your function as a. You then try to assign to it as: a[k] = ... but that doesn't work since a is a scalar...

将一个整数作为a传递给函数,然后尝试将其赋值为:a[k] =…但这是没用的,因为a是一个标量…

It's the same thing as if you had tried:

就好像你曾经尝试过:

50[42] = 7

That statement doesn't make much sense and python would yell at you the same way (presumably).

这个语句没有多大意义,python也会以同样的方式对您喊叫(大概)。

Also, ++k isn't doing what you think it does -- it's parsed as (+(+(k))) -- i.e. the bytcode is just UNARY_POSITIVE twice. What you actually want is something like k += 1

而且,++k并没有做你认为它做的事情——它被解析为(+(+(k)))——也就是说,bytcode只是UNARY_POSITIVE两次。实际上你想要的是k += 1

Finally, be careful with statements like:

最后,注意以下语句:

q = q / b

The parenthesis you use with print imply that you want to use this on python3.x at some point. but, x/y behaves differently on python3.x than it does on python2.x. Looking at the algorithm, I'm guessing you want integer division (since you check q != 0 which would be hard to satisfy with floats). If that's the case, you should consider using:

在print中使用的括号意味着要在python3中使用它。x点。但是,x/y在python3里的表现是不同的。x比勾股定理的x大。看一下算法,我猜你想要整数除法(因为你检查q != 0,这很难满足浮点数)。如果是这样的话,你应该考虑使用:

q = q // b

which performs integer division on both python2.x and python3.x.

它在两个python2上执行整数除法。x和python3.x。

#1


15  

You're passing an integer to your function as a. You then try to assign to it as: a[k] = ... but that doesn't work since a is a scalar...

将一个整数作为a传递给函数,然后尝试将其赋值为:a[k] =…但这是没用的,因为a是一个标量…

It's the same thing as if you had tried:

就好像你曾经尝试过:

50[42] = 7

That statement doesn't make much sense and python would yell at you the same way (presumably).

这个语句没有多大意义,python也会以同样的方式对您喊叫(大概)。

Also, ++k isn't doing what you think it does -- it's parsed as (+(+(k))) -- i.e. the bytcode is just UNARY_POSITIVE twice. What you actually want is something like k += 1

而且,++k并没有做你认为它做的事情——它被解析为(+(+(k)))——也就是说,bytcode只是UNARY_POSITIVE两次。实际上你想要的是k += 1

Finally, be careful with statements like:

最后,注意以下语句:

q = q / b

The parenthesis you use with print imply that you want to use this on python3.x at some point. but, x/y behaves differently on python3.x than it does on python2.x. Looking at the algorithm, I'm guessing you want integer division (since you check q != 0 which would be hard to satisfy with floats). If that's the case, you should consider using:

在print中使用的括号意味着要在python3中使用它。x点。但是,x/y在python3里的表现是不同的。x比勾股定理的x大。看一下算法,我猜你想要整数除法(因为你检查q != 0,这很难满足浮点数)。如果是这样的话,你应该考虑使用:

q = q // b

which performs integer division on both python2.x and python3.x.

它在两个python2上执行整数除法。x和python3.x。