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....