Python编码岩,纸,剪刀,蜥蜴和Spock

时间:2016-09-05 00:45:05

标签: python python-3.x

user.physicsBody?.allContactedBodies()

子程序 - 功能正常

total_guess = 0
wins = 0
loss = 0
import random
characters = ["rock", "paper", "scissors", "lizard", "spock"]
computer = characters[random.randint(0,4)]
print(computer)

由于过去的更改最初有效,此代码无效,'无法将'int'对象转换为str隐式

def valid(text, flag):
    error_message= ""
    while True:
        var = input(error_message + text)
        if flag == "s":
            if var.isalpha()==True:
                break
            else:
               error_message = "This is not valid, "
        elif flag =="i":

            if var.isdigit()==True:
                var = int(var)
                break
            else:
                error_message = user_name + " this is not a number, "

        elif flag == "g":
            if var == "rock" or var == "paper" or var == "scissors" or var ==  "lizard" or var == "spock":
                break
            else:
                error_message = user_name + " this is not valid! "
    return(var)

user_name = valid("What is your name?", "s")
num_rounds = valid(user_name +" how many rounds do you want?", "i")

这个重新启动游戏的块代码不起作用,它的目的是再试一次功能

while True:

        player = valid(user_name + """ ,What do you want as your character:
             Rock, paper, scissors, lizard or spock""", "g" )


        while num_rounds > total_guess:
            total_guess = total_guess + 1
            if player == computer:
                print("Draw!")
        #             --------------------------------------------
            elif player == "Rock" or player == "rock":
                if computer == "paper" or computer == "spock" :
                    loss = loss + 1
                    print("You lost ", computer, " beats ", player)
                    print( user_name + " you have won " + wins +" games")

                if computer == "scissors" or computer == "lizard":
                    wins = wins + 1
                    print("You win", player, " beats ", computer)

        #            ---------------------------------------------
            elif player == "Paper" or player == "paper":
                if computer == "scissors" or computer == "lizard":
                    loss = loss + 1
                    print("You lost ", computer, " beats ", player)


                if computer == "rock" or computer == "spock":
                    wins = wins + 1
                    print("You win", player, " beats ", computer)
        #            ---------------------------------------------
            elif player == "Scissors" or player == "scissors":
                if computer =="Spock" or computer == "rock":
                    loss = loss + 1
                    print("You lost ", computer, " beats ", player)


                if computer  =="paper" or computer == "lizard":
                    wins = wins + 1
                    print("You win", player, " beats ", computer)


        #             --------------------------------------------

            elif player == "Lizard" or  player =="lizard":
                if computer =="scissors" or computer == "rock":
                    loss = loss + 1
                    print("You lost ", computer, " beats ", player)


                if computer  == "paper" or computer == "spock":
                    wins = wins + 1
                    print("You win", player, " beats ", computer)


        #             --------------------------------------------
            elif player == "Spock" or player == "spock":
                if computer == "lizard" or computer == "paper":
                   loss = loss + 1
                   print("You lost ", computer, " beats ", player)

                if computer  =="rock" or computer == "scissors":
                    wins = wins + 1
                    print("You win", player, " beats ", computer)


    #             -------------------------------------------

1 个答案:

答案 0 :(得分:0)

这可能会大大缩短,但我不想重写整个程序。这应该按预期工作。

total_guess = 0
wins = 0
loss = 0
import random
characters = ["rock", "paper", "scissors", "lizard", "spock"]
computer = characters[random.randint(0,4)]
print(computer)

def valid(text, flag):
    error_message= ""
    while True:
        var = input(error_message + text)
        if flag == "s":
            if var.isalpha()==True:
                break
            else:
               error_message = "This is not valid, "
        elif flag =="i":

            if var.isdigit()==True:
                var = int(var)
                break
            else:
                error_message = user_name + " this is not a number, "

        elif flag == "g":
            if var == "rock" or var == "paper" or var == "scissors" or var ==  "lizard" or var == "spock":
                break
            else:
                error_message = user_name + " this is not valid! "
    return(var)

user_name = valid("What is your name?", "s")
num_rounds = valid(user_name +" how many rounds do you want?", "i")

while True:
        while num_rounds > total_guess:
            player = valid(user_name + """ ,What do you want as your character:
             Rock, paper, scissors, lizard or spock""", "g" )
            total_guess = total_guess + 1
            if player == computer:
                print("Draw!")
        #             --------------------------------------------
            elif player == "Rock" or player == "rock":
                if computer == "paper" or computer == "spock" :
                    loss = loss + 1
                    print(' '.join(("You lost", computer, "beats", player)))

                if computer == "scissors" or computer == "lizard":
                    wins = wins + 1
                    print(' '.join(("You win", player, "beats", computer)))

            elif player == "Paper" or player == "paper":
                if computer == "scissors" or computer == "lizard":
                    loss = loss + 1
                    print(' '.join(("You lost", computer, "beats", player)))

                if computer == "rock" or computer == "spock":
                    wins = wins + 1
                    print(' '.join(("You win", player, "beats", computer)))

            elif player == "Scissors" or player == "scissors":
                if computer =="Spock" or computer == "rock":
                    loss = loss + 1
                    print(' '.join(("You lost", computer, " beats ", player)))

                if computer  =="paper" or computer == "lizard":
                    wins = wins + 1
                    print(' '.join(("You win", player, "beats", computer)))

            elif player == "Lizard" or  player =="lizard":
                if computer =="scissors" or computer == "rock":
                    loss = loss + 1
                    print(' '.join(("You lost", computer, "beats", player)))


                if computer  == "paper" or computer == "spock":
                    wins = wins + 1
                    print(' '.join(("You win", player, "beats", computer)))

            elif player == "Spock" or player == "spock":
                if computer == "lizard" or computer == "paper":
                   loss = loss + 1
                   print(' '.join(("You lost", computer, "beats", player)))

                if computer  =="rock" or computer == "scissors":
                    wins = wins + 1
                    print(' '.join(("You win", player, "beats", computer)))

        end_game = input("To exit enter N, to play again enter any key ")
        if end_game == 'n' or end_game == 'N':
            print("THANKS FOR PLAYING " + user_name + '!')
            break
        total_guess = 0