Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13891

UnboundLocalError: why will it work 3/4 times? but give me invaild outputs and an error message 1/4 times? [closed]

$
0
0
from breezypythongui import EasyFrameimport random class RPS(EasyFrame):    def __init__(self):             EasyFrame.__init__(self, title = "Rock, Paper, Scissors game")            self.addLabel(text = "Choose one:", row = 0, column = 1, sticky = "NSEW")            self.rockButton = self.addButton(text = "Rock", row = 1, column = 0, command = self.rock)            self.paperButton = self.addButton(text = "Paper", row = 1, column = 1, command = self.paper)            self.scissorButton = self.addButton(text = "Scissors", row = 1, column = 2, command = self.scissors)            self.addLabel(text = "Computers choice:", row = 2, column = 1)            self.compguess = self.addTextField(text = "", row = 2, column = 2, width = 10)            self.addLabel(text = "Result:", row = 3, column = 1)            self.outcomeField = self.addTextField(text = "", row = 3, column = 2, sticky = "NSEW", width = 10)    def compChoice(self):        num = random.randint(0,9)        if num in range(0,3):            self.computerChoice = "Rock"            self.compguess.setText(self.computerChoice)        elif num in range(3,6):            self.computerChoice = "Paper"            self.compguess.setText(self.computerChoice)        elif num in range(6,9):            self.computerChoice = "Scissors"        self.compguess.setText(self.computerChoice)        return self.computerChoice    def rock(self):        computerChoice = self.compChoice()        if computerChoice == "Rock":            result = "Tie"        elif computerChoice == "Paper":            result = "You lost =("        elif computerChoice == "scissors":            result = "Winner!!"        self.outcomeField.setText(result)    def paper(self):        computerChoice = self.compChoice()        if computerChoice == "Rock":            result = "Winner!!"        elif computerChoice == "Paper":            result = "Tie"        elif computerChoice == "scissors":            result = "You lost =("         self.outcomeField.setText(result)    def scissors(self):        computerChoice = self.compChoice()        if computerChoice == "Rock":            result = "you lost =("        elif computerChoice == "Paper":            result = "Winner!!"        elif computerChoice == "scissors":            result = "Tie"         self.outcomeField.setText(result)def main():    RPS().mainloop()if __name__ == "__main__":    main()

Trying to produce a Rock, Paper, Scissors game and it will work for 3 or 4 tries then hits me with invalid outputs and the error message:UnboundLocalError: local variable "result" referenced before asignment

the issue seems to be with this line:self.outcomeField.setText(result)

but why will it work 3/4 times?


Viewing all articles
Browse latest Browse all 13891

Trending Articles