0

I am building a short game (for a study project). The game is meant to be run in Python Shell (using 3.6.1).

The issue I am having is upon "quit" (exit the game). If a users types "quit" during an input prompt, the game exits. The functionality is fine, however if the user than RESTARTS the game, the list used to hold user data remains populated. It is critical that the list get emptied, and I tried setting list = [] and list = NONE, but neither emptied the list. What gives?

Here is a much condensed version of the code:

import sys
class Game(object):

myList = [] #init list

def inflate_list(self):
    for x in range(0, 10):
        self.myList.append([x]) #just putting x into the list (as example)
    print(self.myList)
    self.run_game()

def check_user_input(self, thisEntry):
    try:
        val = int(thisEntry)#an integer was entered
        return True

    except ValueError: #needed because an error will be thrown
        #integer not entered

        if thisEntry == "quit":
            #user wants to quit
            print("..thanks for playing")

            #cleanup
            self.thisGame = None
            self.myList = []
            del self.myList

            print("..exiting")
            #exit
            sys.exit()

        else:
            print("Invalid entry. Please enter a num. Quit to end game")
            return False    

def run_game(self):

    #init
    getUserInput = False

    #loop
    while getUserInput == False:

        #check and check user's input
        guess = input("Guess a coordinate : ")
        getUserInput = self.check_user_input(guess)

        print (guess, " was entered.")

#start game
thisGame = Game()
thisGame.inflate_list()

Run Example

>>>thisGame = Game()
>>>thisGame.inflate_list()
[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
Guess a coordinate : aaaaa
Invalid entry. Please enter a coordinate. Quit to end game
aaaaa  was entered.
Guess a coordinate : quit
..thanks for playing
..exiting
>>>thisGame = Game()
>>>thisGame.inflate_list()
[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
Guess a coordinate : 

The second time the game is started, the list still holds data....

Ivan Kolesnikov
  • 1,787
  • 1
  • 29
  • 45
dankinship
  • 19
  • 1
  • 8

1 Answers1

1

Change this line:

myList = [] #init list

to this:

def __init__(self):
    self.myList = [] #init list

(After that fix, none of the "cleanup" should be needed.)

As @JoshLee pointed out in the comments above, this Stack Overflow question is a good place to learn about the difference between a class attribute and an instance attribute: Python Class Members.

user94559
  • 59,196
  • 6
  • 103
  • 103
  • Thanks smarx. That worked. .... So any comment as to why a member variable (in a Class) holds the data (even after deletion etc) while putting the variable into the __init__ method produces the expected functionality? – dankinship Jul 07 '17 at 02:57
  • https://stackoverflow.com/questions/12409714/python-class-members read the difference between class attribute vs instance attribute – OLIVER.KOO Jul 07 '17 at 06:20