Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8890543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:31:36+00:00 2026-06-14T22:31:36+00:00

I’m testing out a bit of a code that I’m going to use for

  • 0

I’m testing out a bit of a code that I’m going to use for a simple game but I get this error in init gamefield[x][y] = tecken TypeError: 'field' object does not support indexing

The game is somewhat similar to snake and What I want my program to do is to first create a gamefield which is a matrix where I want to insert my snake(I call it worm here) which is represented by “+”, the position should be chosen randomly.

I then want to be able to decide in which direction the worm grow hence the grow function.

Can anyone see what’s the problem in here? Any help would be greatly appreciated!

import random

class field:
    def __init__(self):

        self.table= [ [ "0" for i in range(10) ] for j in range(10) ]



def printfield(self):
    for row in self.table:
            print (row)
class worm:
    def __init__(self,tecken):

        x = random.randint(1,9) 
        y = random.randint(1,9)
        gamefield[x][y] = tecken

    def grow(self,p,b,c):             
            try :               

                for antal in range(p):
                    if p != 0:      
                        gamefield[x-antal*b][y-antal*c] = "+"    

            except IndexError :
                print ("Game Over")


p = 2
b = 3
c = 0
gamefield = field()
hilda = worm("+")
hilda.grow(p,b,c)

print(gamefield.printfield)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:31:37+00:00Added an answer on June 14, 2026 at 10:31 pm

    The list is gamefield.table. gamefield itself is not indexable.

    If you want to, you can define __getitem__ and __setitem__, so as to wrap the operations on table. Alternatively, you can simply instantiate gamefield as your list of lists instead of making it a separate type of object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.