Python Forum
'int' object does not support item assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'int' object does not support item assignment
#1
Can someone correct that kind of error?



def new(app):
    app=0
    for i in range(10):
      app[i]=i+1
      print(app)
Reply
#2
(Aug-13-2019, 12:17 PM)shane1236 Wrote: Can someone correct that kind of error?
In order to fix it, one need to know what expected output is. Currently that One is just you.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
You initialized app as an integer in app=0. Later in the code you called app in app[i], which you can only do with list items and dictionaries, not integers.

I'm not 100% sure what you were trying to do, but if you want to create a list of numbers from 1 to 10, I would suggest initializing app as a list with app = [], and then using the .push() method to add items to the list.

I hope that helps!
Reply
#4
yes I want the same as you said can you just code it for me. Also it runs when I put app=[], however, it doesnot print anything can you look into that??

(Aug-13-2019, 12:33 PM)buran Wrote: I want the same as below post said, however, Also it runs when I put app=[], however, it doesnot print anything can you look into that??
Reply
#5
(Aug-13-2019, 01:09 PM)shane1236 Wrote: yes I want the same as you said can you just code it for me. Also it runs when I put app=[], however, it doesnot print anything can you look into that??

(Aug-13-2019, 12:33 PM)buran Wrote: I want the same as below post said, however, Also it runs when I put app=[], however, it doesnot print anything can you look into that??

Sorry, I made a mistake. You should use the .append() method (not the .push() method, that is from JavaScript Silenced).

Did you forget to call the function?

def new():
    app = []
    for i in range(10):
      app.append(i+1)
    print(app)

new()
I wouldn't have passed "app" in as a parameter to the function, as you did. I suspect you also meant to "print(app)" only once, instead of putting it inside the for loop.

By the way, this might be a bit more advanced, but for future reference, you can cut all those lines down to one by using a list comprehension as such:

app = [i for i in range(1, 11)]
print(app)
And that will give you the same output Wink
Reply
#6
print(list(range(1,11)))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove an item from a list contained in another item in python CompleteNewb 19 5,738 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  Error: int object does not support item assignment ankita_nthu 2 13,975 Jul-07-2019, 02:14 PM
Last Post: ankita_nthu
  How do I use this? TypeError: 'NoneType' object does not support item assignment ejected 9 22,318 Mar-26-2019, 05:06 AM
Last Post: ejected
  ValueError: This COM object does not support events. meenakshi11 0 3,906 Mar-08-2018, 09:42 AM
Last Post: meenakshi11
  'str' object does not support item assignment SolaVitae 1 9,814 Jul-28-2017, 06:03 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020