在循环完成后,我如何计算获胜次数。
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'发布于 2012-07-29 10:16:08
@kaveman的答案是正确的。我只想指出,通过使用字典并从所有if语句中去掉重复的print 'CPU: ' + cpu行,可以使您的代码更加简洁。
这段代码还检查用户的输入是否有效,如@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-29 10:13:30
您可以跟踪cpu和human的wins变量,并在每次记录win时递增。例如。
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-29 10:46:06
以下是一个经过清理的版本;希望它具有启发性:
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()https://stackoverflow.com/questions/11706069
复制相似问题