if statement - Logical error in my Python program? -


i attempting write simple program simulate number of rock paper scissors games, , return number of wins each item has, based on number of games user puts in simulate. however, prints out number of draws have occurred, , 0 rock, paper, , scissors categories. have feeling logic, haven't been able figure out in last 20 mins. also, if find formatting issues or things better this, please feel free let me know. isn't homework, small practice program. guys!

#simulates games of rock paper scissors , tracks them random import randint   class tracker:     def __init__(self, games):           self.wins = {'rock': 0, 'paper': 0, 'scissors': 0, 'draw':0 }         self.games = games        def runsim(self):         def __init__(self):             pass          game in range(0, self.games):             keys = ['rock', 'paper', 'scissors']              opp1 = keys[randint(0, 2)]             opp2 = keys[randint(0, 2)]               if (opp1 == opp2):                 self.wins['draw'] = self.wins['draw'] + 1             elif ((opp1 or opp2) == 'rock') , ((opp1 or opp2) == 'scissors'):                 self.wins['rock'] = self.wins['rock'] + 1             elif ((opp1 or opp2) == 'paper' , (opp1 or opp2) == 'scissors'):                 self.wins['scissors'] = self.wins['scissors'] + 1             elif ((opp1 or opp2) == 'paper' , (opp1 or opp2) == 'rock'):                 self.wins['paper'] = self.wins['paper'] + 1          print 'draws: ', self.wins['draw']         print 'rocks: ', self.wins['rock']         print 'papers: ', self.wins['paper']         print 'scissors: ', self.wins['scissors']         print keys   while true:     print 'enter number of games: '     games = raw_input()     games = int(games)       tracker = tracker(games)     tracker.runsim() 

the following not think does:

(opp1 or opp2) == 'rock' 

you'll want write following instead:

(opp1 == 'rock' or opp2 == 'rock') 

what (opp1 or opp2) == 'rock' is:

  • evaluate (opp1 or opp2) , compare 'rock'.

given opp1 , opp2 both non-empty strings expression (opp1 or opp2) evaluate true. you're comparing true == 'rock'.


Comments