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 8294349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:10:55+00:00 2026-06-08T14:10:55+00:00

Using Python, and classes, I’m trying to make a game like battleship, mostly for

  • 0

Using Python, and classes, I’m trying to make a game like battleship, mostly for practice. So I made a game Board object, and you “see” it, and “change” it. (I’ll merge “create” in with init later)

But something weird is happening where my “Change” is applying to every row…here is the code:

class Board:
    'Game Board'
    topMarkers = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
    sideMarkers = list(range(0, 26))


    def __init__(self,h,w): #Makes the map
        self.height = h
        self.width = w


    def create(self): #Makes a map for displaying
        wave = '~'
        self.row = []
        self.column = []
        #self.column = wave * self.width # If width is 4, column is now '~~~~'

        for c in range(self.width):
            self.column.append(wave)


        raw_input(self.column)

        for r in range(self.height): #
            self.row.append(self.column)

        raw_input(self.row)

    def showGrid(self):

        print self.row

    def changeRow(self, y, x):
        self.row[1][2] = "Test"







yourShipsMap = Board(4,3)

theirShipsMap = Board(4,7)


theirShipsMap.create()

theirShipsMap.changeRow(2,2)
theirShipsMap.showGrid()

When I run this, it’s writing “Test” in not just the first list, second index, but in every list, second index. Any idea why it’s doing that??

I hate when I stop programming all together for like a month and forget everything.

  • 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-08T14:10:56+00:00Added an answer on June 8, 2026 at 2:10 pm

    You create only one column, then append it to your rows. Appending a list to another list does not make a new copy, but re-uses the same list over and over.

    Append a copy instead by using the [:] slice syntax or a list() call:

    for r in range(self.height): #
        self.row.append(self.column[:])
    

    Here is an example of what happens for you:

    >>> column = [1, 2]
    >>> row = []
    >>> row.append(column)
    >>> row.append(column)
    >>> column[0] = 'foobar'
    >>> row
    [['foobar', 2], ['foobar', 2]]
    

    If we use a copy on the other hand, the copies do not change when we alter the original:

    >>> row = []
    >>> row.append(list(column))
    >>> row.append(column[:])
    >>> row
    [['foobar', 2], ['foobar', 2]]
    >>> column[0] = 1
    >>> row
    [['foobar', 2], ['foobar', 2]]
    

    I used both methods of creating a (shallow) copy of the original columns list in the above example. If you need to create a deep copy (a list containing other mutable items such as lists or dicts), use the copy.deepcopy utility instead.

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

Sidebar

Related Questions

Using Python I would like to find the date object for last Wednesday. I
I'm trying to expose one of my classes to python using boost's python module.
I'm trying to expose my C++ Classes to Python using Boost.Python. Here is a
I'm using new style classes in Python 2.6 and am having trouble with __getattr__
Using Python, how does one parse/access files with Linux-specific features, like ~/.mozilla/firefox/*.default ? I've
I am planning to implement C++-like constructor/destructor functionality to one of my Python classes
I am very new to Python. Not learnt classes yet. Using Python 3.2.2. Tried
I am trying to develop a plasmoid using python. I have tried eclipse with
I have a plugin written entirely in Python using PyObjC whose core classes I
Here's how I am declaring constants and using them across different Python classes: #

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.