python - How do I keep score in a rock paper scissors game? -


i'm starting python , writing simple rock paper scissors game. here's have far. can play game player perfectly. want know how keep score of wins, if player 1 wins first game say:

player 1 = 1 player 2 = 0

and on. if there's tips make code more efficient (with nice explanation), nice.

thanks!

consider pseudo-code suggestions (some parts need implemented):

# player class. should have name , score class player():    def __init__(name):       self.name = name       self.score = 0  # displays prompt , reads in players name returning string def getplayername():    # needs code here, see next function idea of    pass  # ask player make choice, takes player object # , returns string def getplayerattack(player):     print "%s, choose?" % player.name     return raw_input("> ")  # determines wins , updates score accordingly # takes in player objects , attack choices def attack(player1, choice1, player2, choice2):      if choice1 == choice2:        print "its's tie."     elif choice1 == "1" , choice2 == "2":        print "%s wins." % player2        player2.score = player2.score + 1     elif ... # other attacks  # display scores of 2 players def displayscores(player1, player2):     print "%s vs %s" % (player1.score, player2.score)  player1 = player(getplayername()) player2 = player(getplayername()) while true:     choice1 = getplayerattack(player1)     choice2 = getplayerattack(player2)     attack(player1, choice1, player2, choice2)     displayscores(player1, player2) 

this need work, , it's not super-optimal, should start , should show more concepts. use ctrl-c stop or add stop condition (e.g. either player enters "0") -- bugs included free. :)

happy coding.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -