-2

and i made a rock paper scissor game, but when i loose a life or win, it keep on spamming the same message can anyone help

import random #this allows me to generate random numbers
def main(): #all the main code is under this
    print ("Hello Welcome to my Rock, Paper and Scissors game!") #welcome message
    count = 0 #This counts how many tries you tried
    while count != 3: #You can only try 3 times
        print ("Please choose whether:") #allows the user to choose between rock, paper and scissors
        print ("[1] Rock")
        print ("[2] Paper")
        print ("[3] Scissors")
        user_choice = int(input()) #saves user input into a variable
        if user_choice == 1:
            user_choice = ("Rock")
        elif user_choice == 2:
            user_choice = ("Paper")
        elif user_choice == 3:
            user_choice = ("Scissors")
        elif user_choice > 3:
            print ("Invalid choice!")
            print ("Restarting...")
            main()

        computer_choice = random.randint(1,3) #generates random number between 1 and 3
        if computer_choice == 1: #define 1,2 and 3
            computer_choice = ("Rock")
        elif computer_choice == 2:
            computer_choice = ("Paper")
        elif computer_choice == 3:
            computer_choice = ("Scissors")
        def choice_1():
            score = 0
            lives = 2
            if user_choice == "Rock":
                if user_choice == computer_choice:
                    print ("You chose:",user_choice)
                    print ("--------VS--------")
                    print ("Computer chose:",computer_choice)
                    print ("Draw!")
                elif user_choice == "Rock" and computer_choice == "Paper":
                    print ("You chose:",user_choice)
                    print ("--------VS--------")
                    print ("Computer chose:",computer_choice)
                    print ("You lost!")
                    lives = lives - 1
                elif user_choice == "Rock" and computer_choice == "Scissors":
                    print ("You chose:",user_choice)
                    print ("--------VS--------")
                    print ("Computer chose:",computer_choice)
                    print ("You Won!")
                    score = score + 1

            if user_choice == "Paper":
                if user_choice == computer_choice:
                    print ("You chose:",user_choice)
                    print ("--------VS--------")
                    print ("Computer chose:",computer_choice)
                    print ("Draw!")
                elif user_choice == "Paper" and computer_choice == "Scissors":
                    print ("You chose:",user_choice)
                    print ("--------VS--------")
                    print ("Computer chose:",computer_choice)
                    print ("You lost!")
                    lives = lives - 1
                elif user_choice == "Paper" and computer_choice == "Rock":
                    print ("You chose:",user_choice)
                    print ("--------VS--------")
                    print ("Computer chose:",computer_choice)
                    print ("You Won!")
                    score = score + 1

            if user_choice == "Scissors":
                if user_choice == computer_choice:
                    print ("You chose:",user_choice)
                    print ("--------VS--------")
                    print ("Computer chose:",computer_choice)
                    print ("Draw!")
                elif user_choice == "Scissors" and computer_choice == "Rock":
                    print ("You chose:",user_choice)
                    print ("--------VS--------")
                    print ("Computer chose:",computer_choice)
                    print ("You lost!")
                    lives = lives - 1
                elif user_choice == "Scissors" and computer_choice == "Paper":
                    print ("You chose:",user_choice)
                    print ("--------VS--------")
                    print ("Computer chose:",computer_choice)
                    print ("You Won!")
                    score = score + 1
            print ("You have",lives,"lives left")
            while lives != 0:
                choice_1()
            else:
                print ("oops! you ran out of lives")
                print ("Restarting...")
                main()
            count = count + 1
        choice_1()

    else:
        print ("Thanks for playing!")
        if score == 3:
            print ("Fantastic! you scored:",score,"/3")
        elif score == 2:
            print ("Good job! you scored:",score,"/3")
        elif score == 1:
            print ("Not bad! you scored:",score,"/3")
        elif score == 0:
            print ("Unlucky! you scored:",score,"/3")




main()
Kevin
  • 74,910
  • 12
  • 133
  • 166
Speed Demo
  • 21
  • 5
  • When you call `main()` from within `main`, you then have two instances of `main` on the call stack. When the newer one finishes executing (perhaps when the game ends), control returns to the older `main` and continues where it left off. Depending on what choices the user makes, you might have hundreds of instances of main waiting to run. Rather than having a function recursively call itself to return to the start, control the flow of the program using loops and `continue/break`. – Kevin Jan 27 '16 at 18:44
  • @Kevin great answer, why not actually make it an answer? xD – Rob Murray Jan 27 '16 at 18:45
  • so what should i add or modify to the while statement – Speed Demo Jan 27 '16 at 18:46
  • I didn't make it an answer because it's not very actionable; the reader learns what _not_ to do, but doesn't specifically know what to do, other than generally "use continue/break". It leaves them wondering "so how should I modify my existing code?" – Kevin Jan 27 '16 at 18:47
  • @kevin thats my question – Speed Demo Jan 27 '16 at 18:48
  • Don't define functions inside other functions. – Daniel Jan 27 '16 at 19:09

1 Answers1

0

You should either break out of the loop before calling the main() function or restructure your program in some way so that more than one instance of main() is not running at the same time. That is one of the dangers of calling a function from inside of itself. There are other threads that discuss recursive function calling if you want to read them.

Community
  • 1
  • 1
Mike C.
  • 1,761
  • 2
  • 22
  • 46