0

我正在尝试编写一个简单的程序,该程序将模拟许多石头剪刀布游戏,并根据用户放入模拟的游戏数量返回每个项目的获胜次数。但是,它只打印出已发生的抽奖次数,而石头、纸和剪刀类别则为 0。我觉得这与我的逻辑有关,但在过去的 20 分钟内无法弄清楚。此外,如果您发现任何格式问题或我可以在这方面做得更好的事情,请随时告诉我。这也不是家庭作业,只是一个小练习程序。多谢你们!

#Simulates games of rock paper scissors and tracks them
from 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

        for 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') and ((opp1 or opp2) == 'scissors'):
                self.wins['rock'] = self.wins['rock'] + 1
            elif ((opp1 or opp2) == 'paper' and (opp1 or opp2) == 'scissors'):
                self.wins['scissors'] = self.wins['scissors'] + 1
            elif ((opp1 or opp2) == 'paper' and (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()
4

1 回答 1

5

以下不做你认为它做的事:

(opp1 or opp2) == 'rock'

您需要编写以下内容:

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

什么(opp1 or opp2) == 'rock'是:

  • 评估(opp1 or opp2)并将其与'rock'.

鉴于opp1andopp2都是非空字符串,表达式(opp1 or opp2)将计算为True。所以你基本上是在比较True == 'rock'

于 2013-05-05T23:02:23.107 回答