0

循环完成后如何计算胜利。

print 'PLAY ROCK PAPER SCISSORS'

for roundd in range(1,6):
    print 'Round: ' + str(roundd)
    human = raw_input('Whats your draw: ')
    from random import choice
    cpu = choice(('rock', 'paper', 'scissors'))

    if human == cpu:
        print 'CPU: ' + cpu
        print 'Draw'

    if cpu == 'rock' and human == 'scissors':
        print 'CPU: ' + cpu
        print 'You Lose'
    if cpu == 'scissors'and human == 'paper':
        print 'CPU: ' + cpu
        print 'You Lose'
    if cpu == 'paper'and human == 'rock':
        print 'CPU: ' + cpu
        print 'You Lose'

    if cpu == 'rock' and human == 'paper':
        print 'CPU: ' + cpu
        print 'You Win!'
    if cpu == 'scissors'and human == 'rock':
        print 'CPU: ' + cpu
        print 'You Win'
    if cpu == 'paper'and human == 'scissors':
        print 'CPU: ' + cpu
        print 'You Win'
4

3 回答 3

5

@kaveman 的回答是正确的。我只想指出,您可以通过使用字典并从所有语句中删除重复的print 'CPU: ' + cpu行来使您的代码更加简洁。if

正如@atomicinf 所建议的那样,此代码还检查用户的输入是否有效。否则,我编写的代码将'lol'被视为自动获胜。:) 这就是while下面的循环所做的:如果用户输入了一个无效的移动,它会给他们一个错误信息并要求他们再试一次,直到他们做了一个有效的移动。

这是进行该更改和其他一些更改的代码,并附有一些关于我为什么要做各种事情的评论:

from random import choice # usually, imports go at the top; easier to manage

print 'PLAY ROCK PAPER SCISSORS'

# This dictionary associates a move to the move that it beats.
beats = {
    'rock': 'scissors',
    'paper': 'rock',
    'scissors': 'paper',
}
moves = ('rock', 'paper', 'scissors') # The tuple of all valid moves
# could also do moves = beats.keys()

draws = cpu_wins = human_wins = 0 # start our counter variables off at 0

for roundd in range(1,6):
    print 'Round: ' + str(roundd)
    human = raw_input("What's your draw: ")
    while human not in moves: # keep retrying if they gave a bad move...
        print "Invalid move '%s' - expected one of %s." % (human, ', '.join(moves))
        # this % formatting just replaces the %s with the variable on the left
        print "Try again!"
        human = raw_input("What's your draw: ")
    cpu = choice(moves)

    print 'CPU: ' + cpu # this happens every time, no need to retype it so many times :)

    if human == cpu:
        print 'Draw'
        draws += 1
    elif human == beats[cpu]:
        print 'You Lose'
        cpu_wins += 1
    else:
        print 'You Win'
        human_wins += 1

# done - print out the overall record
print "Your record: %s wins, %s losses, %s draws" % (human_wins, cpu_wins, draws)

说得通?

于 2012-07-29T02:16:08.123 回答
3

您可以跟踪 和 的wins变量,cpu并在human每次记录获胜时递增。例如

human_wins = 0
cpu_wins = 0

for roundd in range(1,6):
    if cpu == 'paper'and\
       human == 'rock':
        cpu_wins += 1
        print 'CPU: ' + cpu
        print 'You Lose'

    if cpu == 'paper'and\
       human == 'scissors':
        human_wins += 1
        print 'CPU: ' + cpu
        print 'You Win'
    ...
于 2012-07-29T02:13:30.433 回答
2

这是一个清理后的版本;可能有启发性:

import random

class RockPaperScissors(object):
    choices = ['rock', 'paper', 'scissors']

    def __init__(self):
        self.wins   = 0
        self.draws  = 0
        self.losses = 0

    def cpu(self):
        return random.choice(type(self).choices)

    def human(self):
        while True:
            res = raw_input("What's your draw: ").strip().lower()
            if res in type(self).choices:
                return res
            else:
                print('Enter one of {}'.format(', '.join(type(self).choices)))

    def win(self):
        print('You win!')
        self.wins += 1

    def draw(self):
        print('Draw')
        self.draws += 1

    def lose(self):
        print('You lose')
        self.losses += 1

    def play(self):
        """
        Play one hand
        """
        human = self.human()
        cpu   = self.cpu()
        print("Computer chose {}".format(cpu))
        val   = type(self).choices.index
        [self.draw, self.lose, self.win][(val(cpu) - val(human)) % 3]()

def main():
    print('PLAY ROCK PAPER SCISSORS')
    rps = RockPaperScissors()

    for rnd in xrange(1,6):
        print('Round: {}'.format(rnd))
        rps.play()

    print('Final tally: {} losses, {} draws, {} wins'.format(rps.losses, rps.draws, rps.wins))

if __name__=="__main__":
    main()
于 2012-07-29T02:46:05.980 回答