0

I want to code a rock paper scissors game with a scoring system that allows the user to wish to play again and the score adds on and not restarting to 0.

My code is here: https://pastebin.com/eRqEuwtY (It's also attached on this message)

Your help is appreciated.

import random
score1 = int(0)
score2 = int(0)

def main():
    while True:

        player = input('What do you choose to play (R, P, S)? ')
        computer = random.choice(["R", "P", "S"])
        print("You chose", (player), "and the computer chose", (computer))

        if computer == player:
            print("It's a tie")
            print("Score: You =", score1, "Computer =", score2)

        if computer == "R" and player == "S":
            print("Computer wins")
            print("Score: You =", score1, "Computer =", score2 + 1)

        if computer == "P" and player == "R":
            print("Computer wins")
            print("Score: You =", score1, "Computer =", score2 + 1)

        if computer == "S" and player == "P":
            print("Computer wins")
            print("Score: You =", score1, "Computer =", score2 + 1)

        if computer == "S" and player == "R":
            print("You won")
            print("Score: You =", score1 + 1, "Computer =", score2)

        if computer == "R" and player == "P":
            print("You won")
            print("Score: You =", score1 + 1, "Computer =", score2)

        if computer == "P" and player == "S":
            print("You won")
            print("Score: You =", score1 + 1, "Computer =", score2)

        play_again = input("Would you like to play again? Y/N ")

        if play_again == "Y":
            main()
        else:
            print("You scored", score1, "and the computer scored", score2)
            exit()

main()
MaJoR
  • 954
  • 7
  • 20
Anthony B
  • 11
  • 2
  • 2
    You haven't coded a loop, you've coded an endless recursion. Do NOT call main() to play again. Instead, change the condition of your while loop to stop if the last input isn't Y. – Roland Weber Nov 21 '18 at 12:13
  • Also, the formatting of the code in your question could be improved. Some of it appears as plain text instead of a formatted code block. – Roland Weber Nov 21 '18 at 12:14
  • Thanks for the feedback. I'm new to python and I don't really understand what you said. Is there a way that it can be demonstrated?? – Anthony B Nov 21 '18 at 12:21

2 Answers2

1

Your problem is that you print the score + 1 if you win. This does not save the new score to the variable!

Example:

print("You won")
score += 1
print("Score: You =", score1, "Computer =", score2)

Another problem in your code is that you call the main function again every time the user wants to play again. This is endless recursion and will result in an error if you hit the recursion limit. It's better to do it like this:

def main():
    play_again = "Y"
    while play_again == "Y":
        #code...
        play_again = input("Would you like to play again? Y/N ")
    print("You scored", score1, "and the computer scored", score2)

main()
Jasper
  • 1,795
  • 1
  • 12
  • 17
0

As Jasper pointed out, you need to save the new score to the respective variable. Here is a rework of your code, including some improvements:

import random


def main():

    SYMBOLS = ["R", "P", "S"]
    INPUTS_YES = ["Y", "YES", "I LOVE THIS GAME SO MUCH"]
    INPUTS_NO = ["N", "NO", "PLZ STOP THIS"]
    INPUTS_ALL = INPUTS_YES + INPUTS_NO

    keep_playing = True
    score_player = 0
    score_computer = 0

    while keep_playing:

        symbol_player = input("What do you choose to play? (R, P, S)").upper()
        while not symbol_player in SYMBOLS:
            print("invalid input")
            symbol_player = input("What do you choose to play? (R, P, S)").upper()

        symbol_computer = random.choice(SYMBOLS)
        print("You chose ", symbol_player, " and the computer chose ", symbol_computer)

        difference = (SYMBOLS.index(symbol_player) - SYMBOLS.index(symbol_computer)) % 3

        if difference == 0:
            print("It's a tie")    

        if difference == 1:
            score_player += 1
            print("You won")    

        if difference == 2:
            score_computer += 1
            print("Computer won")

        print("Score: You = ", score_player, ", Computer = ", score_computer)

        play_again = input("Would you like to play again? Y/N").upper()

        while not play_again in INPUTS_ALL:
            print("invalid input")
            play_again = input("Would you like to play again? Y/N").upper()

        if play_again in INPUTS_NO:
            keep_playing = False

    print("You scored", score_player, "and the computer scored", score_computer)


if __name__ == "__main__":
    main()
saucecode
  • 39
  • 4