-2

Need help on stopping the score from resetting every game,

this is my code so far and its not working.

This is part of a function that checks who's the winner etc.

def winner ():
        global Win
        if alist [0] == player1 and alist[1] == player1 and alist[2] ==player1:#top line horizontal
            Win = 'player1'
            return True
        elif alist[3] == player1 and alist[4] == player1 and alist[5] ==player1:#middleline horizontal
            Win = 'player1'
            return True

this statement determines the score and whether to start another game.

if winner():
                if Win == 'player1':
                    print("player1 is winner")
                    p1score = p1score+1
                elif Win == 'player2':
                    print("player2 is winner")
                    p2score = p2score+1
                print('Player1s score =', p1score,'Player2s score =', p2score)
print("Would you like to play again(yes or no)")
                restart = input("")
                if restart == 'yes':
                    return gamemode()

Ok so at the end of the game the score displays correctly, but when another game is played it resets?

def playervscomputer():
        global Player1Score
        Player1Score = 0
        global ComputerScore
        ComputerScore = 0
        players = [name, 'computer']
        global turn
        turn = random.randint(0,1)
        while True:
            print('its\s %s\'s turn' % players[turn])
        if winner1():
            #Check if people have won
            if Win == 'player1':
                print("player1 is winner")
                Player1Score = Player1Score+1
                print("player1s score is", Player1Score, 'Computer Score=', ComputerScore)
                print("would you like to play again?(yes or no)")
                restart = input("")
                if restart =='yes':
                    return main()
                else:
                    print("Thanks for playing")
     elif Win == 'Computer':
                    print("Computer is winner")
                    ComputerScore = ComputerScore+1
                print('Player1s score =', Player1Score,'Computers score =', ComputerScore)

Any ideas or any help on it keeping the scores after a few games have been played.

Thanks

Oscar Dolloway
  • 199
  • 3
  • 17
  • 1
    There is way too less information to answer the question. Where does the variable p1score live? Global? Inside a function? What does the function gamemode do? – Dschoni Jan 22 '16 at 14:30
  • I'll add some more code later on when I have access to my computer, I didn't want to add too much code looks like I haven't added enough. I've got p1 score already as global – Oscar Dolloway Jan 22 '16 at 17:00

2 Answers2

0

Looks like you're not fully understanding scope. Try changing p1score and p2score to global variables (like you did with Win).

This YouTube video helped me understand the difference between global and local variables, maybe it will help you. https://www.youtube.com/watch?v=A054Ged9suI

Thomas
  • 603
  • 7
  • 12
0

A simple programm, that uses a function to increment two independent variables (in a list):

score = [0,0]
def increment(Player):
    global score
    if Player == 1:
        score[0]+=1
    elif Player ==2:
        score[1]+=1
    else:
        print('Input not defined')
def main():
    global score
    print(score)
    increment(1)
    increment(2)
    increment(2)
    print(score)
Dschoni
  • 3,714
  • 6
  • 45
  • 80