vendredi 17 novembre 2017

How to make variable values carry over outside of if statement

So, I've made a rock paper scissors program, and it works like a charm. The only gripe I have with it is that the scores in the output do not carry over afterwards. This is written in Python- and forgive me if the code in the box below is formatted wrong, I'm new to stack overflow.

ties = ties + 1, for example, will print properly, but then when the program is run again, if I get another tie, it won't say ties = 2, it'll keep saying Ties: 1. Or if I get a tie and a loss, it'll say Ties: 1 Loses: 0 and then it'll print Ties: 0 Loses: 1

import random
def main():


    playRPS = str.lower(input("Play a game or rock paper scissors?  Yes or no.  "))
    if(playRPS == "no"):
        print("Well, why did you run the program them?")
    while(playRPS == "yes"):
        human = int(input("Welcome to rock paper scissors!  Type 1 for rock, 2 for paper, or 3 for scissors"))
        cpu = random.randint(1,3)
        ties = 0
        wins = 0
        loses = 0
        if(human == cpu):
            print("It's a tie!")
            ties = ties + 1
            playRPS = input("Play again?")
        elif(human == 1 and cpu == 2):
            print("The computer picked paper, you picked rock, CPU wins!")
            loses = loses + 1
            playRPS = input("Play again?")
        elif(human == 1 and cpu == 3):
            print("The computer picked scissors, you picked rock, You win!")
            wins = wins + 1
            playRPS = input("Play again?")
        elif(human == 2 and cpu == 1):
            print("You picked paper, the computer picked rock, You win!")
            wins = wins + 1
            playRPS = input("Play again?")
        elif(human == 2 and cpu == 3):
            print("You picked paper, the computer picked scissors, CPU wins!")
            loses = loses + 1
            playRPS = input("Play again?")
        elif(human == 3 and cpu == 1):
            print("You picked scissors, the computer picked rock, CPU wins!")
            loses = loses + 1
            playRPS = input("Play again?")
        elif(human == 3 and cpu == 2):
            print("You picked scissors, the computer picked paper, You win!")
            wins = wins + 1
            playRPS = input("Play again?")
        else:
            print("An error occured, maybe you typed 4, or 5, or maybe you made a typo.")
        playRPS = str.lower(input("Another game?  Yes or no."))
        print("Wins:  " + str(wins))
        print("Ties:  " + str(ties))
        print("Loses  " + str(loses))

main()

Aucun commentaire:

Enregistrer un commentaire