0

I have written the below code for a rock, paper, scissors game as my first bit of independent coding. I've tried every possible solution I can possibly think of to get a cumulative running total of wins, losses and draws, however every time it only shows me the most recent result - i.e. 1 win 0 losses 0 draws even on the 50th game if the most recent game was a win.

Can anybody suggest a way to solve this?

# Rock, Paper, Scissors!

import random
import time

print('Welcome to Rock, Paper, Scissors, a Python recreation of the classic game.')
print('The computer will randomly choose a sign at the start of the run for you to try to beat.')
print('Choose carefully...')


def fullgame():
    compguess = random.randint(1, 3)
    if compguess == 1:
        compsign = 'rock'
    elif compguess == 2:
        compsign = 'paper'
    else:
        compsign = 'scissors'
    playsign = input('Would you like to throw rock, paper or scissors? ')
    while playsign != 'rock' and playsign != 'paper' and playsign != 'scissors':
        print('You need to pick a valid sign!')
        playsign = input('Would you like to throw rock, paper or scissors? ')

    def numberallocator(rps):
        if rps == 'rock':
            return 1
        elif rps == 'paper':
            return 2
        elif rps == 'scissors':
            return 3
        else:
            print('That\'s not a valid sign!')
    playguess = numberallocator(playsign)
    numberallocator(playsign)

    def game(cg, pg):
        print('The computer is deciding...')
        time.sleep(1)
        print('The computer throws ' + compsign + ' and you throw ' + playsign + '...')
        time.sleep(1)
        if cg == pg:
            print('It\'s a draw! You both picked ' + compsign)
            return 'd'
        else:
            if cg == 1:
                if pg == 2:
                    print('You win! Paper beats rock!')
                    return 'w'
                elif pg == 3:
                    print('You lose! Rock beats scissors!')
                    return 'l'
            elif cg == 2:
                if pg == 1:
                    print('You lose! Paper beats rock!')
                    return 'l'
                elif pg == 3:
                    print('You win! Scissors beats paper!')
                    return 'w'
            else:
                if pg == 1:
                    print('You win! Scissors beats rock!')
                    return 'w'
                elif pg == 2:
                    print('You lose! Scissors beats paper!')
                    return 'l'
    game(compguess, playguess)


playagain = 'y'

gamesplayed = 0


while playagain == 'y' or playagain == 'Y':
    fullgame()
    gamesplayed = gamesplayed + 1
    print('You have played ' + str(gamesplayed) + ' games.')
    playagain = input('Play again? y/n: ')
    while playagain != 'y' and playagain != 'n':
        print('You must pick y or n!')
        playagain = input('Play again? y/n: ')

print('Thank you for playing rock, paper, scissors! This has been coded in Python by Ethan Lang!')

Thank you!

001
  • 13,291
  • 5
  • 35
  • 66
  • 2
    Your `fullgame()` should have a return indicating if the player won or loss. Keep track of that inside your while loop. For instance, have fullgame() return a 1 if player wins and a 0 if they lose. Then call it inside your while loop like `gameswon += fullgame()` instead of just `fullgame()` all on its lonesome. – JNevill Jun 07 '22 at 20:42
  • I don't see where you are attempting to print any stats except `gamesplayed`. – 001 Jun 07 '22 at 20:42
  • Unrelated to your question: `while playagain == 'y' or playagain == 'Y':` is the same as `while playagain.lower() == 'y':`. This is a general good practice when you want to make understanding text case insensitive. For example, you could use it to read "rock", "paper", "scissors" from user's input, so "rock" would be the same as "Rock" or "ROCK". – Ignatius Reilly Jun 07 '22 at 20:57

1 Answers1

0

If you want to save a variable outside of all the functions, and modify it inside a function, you can use the Python global keyword. Otherwise, when you try to modify the variable inside a function, even if you defined it already outside the function, Python will assume you meant to create a brand-new variable, with its scope only function-level, and the variable with global scope won't be modified. To specify that you want to modify the global variable, you can use

global <variable_name>

as the first line in your function. That way, Python knows that any references to variable_name inside your function refer to the "global" variable (outside the function).

Disclaimer: using global variables is generally regarded as a Bad Idea. It makes it more difficult to fix issues with your code, since any code could possibly be modifying your global variables, which makes it harder to keep track of where it could be going wrong. For more info, see Why are global variables evil?

Wherever possible, you should try to find another solution. In this case, the solution in @JNevill's comment is probably a better idea.

Esther
  • 450
  • 3
  • 9
  • 1
    Globals should be avoided, they make code less readable and difficult to debug and in general are not a good approach. The approach suggested by JNevill is better, an while it may look less intuitive at first, is better for a beginner to get use to it. – Ignatius Reilly Jun 07 '22 at 20:50
  • 1
    @IgnatiusReilly true, that is a pretty good idea. I'll put a warning about globals in the answer, should have had that there before. – Esther Jun 07 '22 at 20:52