Help with some simple python program (rock paper scissors)
Anonymous in /c/coding_help
0
report
I'm doing a python course, and I wanted to do a practice programm (rock papar scissors). Somehow I'm getting the error that the variable "x" is not defined, even though I already defined it. Anyone knows why?<br><br>​<br><br>```<br>import random<br><br>class Player():<br> def __init__(self, name):<br> self.name = name<br> self.lives = 2;<br> self.wins = 0<br><br> def attack(self, enemy):<br> random = random.randint(1,3)<br> if(random == 1):<br> return('Rock')<br> elif(random == 2):<br> return('Paper')<br> else:<br> return('Scissors')<br><br> def is_alive(self):<br> return (self.lives > 0)<br><br>class Game():<br> def __init__(self):<br> self.p1 = Player('Player_one')<br> self.p2 = Player('Player_two')<br><br> def run(self):<br> while(self.p1.is_alive() and self.p2.is_alive()):<br> self.round()<br> print(self.p1.name,' ', self.p1.wins)<br> print(self.p2.name,' ', self.p2.wins)<br><br> def round(self):<br> a1 = self.p1.attack(self.p2)<br> a2 = self.p2.attack(self.p1)<br> print(a1)<br> print(a2)<br> if(a1 == a2):<br> print('Tie')<br> elif(a1 == 'Paper' and a2 == 'Scissors'):<br> self.p2.wins = self.p2.wins+1<br> elif(a1 == 'Rock' and a2 == 'Paper'):<br> self.p2.wins = self.p2.wins+1<br> elif(a1 == 'Scissors' and a2 == 'Rock'):<br> self.p2.wins = self.p2.wins+1<br> elif(a1 == 'Paper' and a2 == 'Rock'):<br> self.p1.wins = self.p1.wins+1<br> elif(a1 == 'Rock' and a2 == 'Scissors'):<br> self.p1.wins = self.p1.wins+1<br> elif(a1 == 'Scissors' and a2 == 'Paper'):<br> self.p1.wins = self.p1.wins+1<br><br>game = Game()<br>game.run()<br>```<br><br>​
Comments (0) 0 👁️