I’m trying to call a function in one class from another class. Here is my code:
import os
class GameRoom():
msgLine1 = ""
msgLine2 = ""
msgLine3 = ""
msgLine4 = ""
msgLine5 = ""
def GameStatus(self):
while True:
os.system('cls')
print self.msgLine1
print self.msgLine2
print self.msgLine3
print self.msgLine4
print self.msgLine5 + "\n"
print "Do what?\n"
userDecision = raw_input()
if userDecision.upper() == "GO":
Player().Go()
break
else:
self.CycleMessages("That's not a valid choice!")
def CycleMessages(self,newMsg="error"):
self.msgLine5 = self.msgLine4
self.msgLine4 = self.msgLine3
self.msgLine3 = self.msgLine2
self.msgLine2 = self.msgLine1
self.msgLine1 = newMsg
class Player():
def Go(self):
GameRoom().CycleMessages("Player goes.")
GameRoom().GameStatus()
def main():
GameRoom().GameStatus()
if __name__ == '__main__':
main()
When I call CycleMessages through itself, the lines fill up with ‘That’s not a valid choice!’ normally, like they should. When I call CycleMessages from the Player class, all of the lines clear instead of displaying ‘Player goes.’. ‘error’ doesn’t display either.
I would like ‘Player goes.’ to show up when I call Player().Go(). How do I do this? I appreciate any help!
This is happening because when you call
GameRoom().CycleMessages("Player goes."), you’re calling it on a new instance ofGameRoom. Easy fix:Also, this wasn’t your question, but you might want to consider changing your naming conventions (see: PEP8).