虽然功能问题继续发送垃圾邮件

时间:2016-01-27 18:41:08

标签: python

我制作了一个摇滚纸剪刀游戏,但是当我放弃生命或获胜时,它继续发送垃圾邮件同样的消息可以任何人帮助

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()

1 个答案:

答案 0 :(得分:0)

您应该在调用main()函数之前突破循环,或者以某种方式重构程序,以便多个main()实例不会同时运行。这是从内部调用函数的危险之一。有other threads讨论递归函数调用,如果你想读它们。