创建一个python石头剪刀布游戏,有几个bug

2024-05-16 21:33:45 发布

您现在位置:Python中文网/ 问答频道 /正文

import random

#Rock Paper Scisscors Lizard Spock Game

#Name to number
rock = ()
paper = ()
scisscors = ()
lizard = ()
Spock = ()

def main():
    print("Let's play rocker, paper, scissors, lizard, Spock")
    computer = npc_guess()
    player = user_guess()
    complete(computer, player)

def user_guess(): #Users hand in RPSLS
    player = input("Choose 'rock', 'paper', 'scisscors', 'lizard', 'Spock'")
    game = True
    while game:
        if player == 'rock' or player == 'paper' or player == 'scisscors'\
        or player == 'lizard' or player == 'Spock':
            return player
    else:
        print("That Choice isn't valid.")
user_guess()

def npc_guess(): #computers hand in RPSLS
    while True:
        computer = random.randrange(0, 4)
        if computer == 0:
            print("The NPC has chosen rock")
            computer == rock
        elif computer == 1:
            print("The NPC has chosen paper")
            computer == paper
        elif computer == 2:
            print("The NPC has chosen scisscors")
            computer == scisscors
        elif computer == 3:
            print("The NPC has chosen lizard")
            computer == lizard
        elif computer == 4:
            print("The NPC has chosen Spock")
            computer = Spock
        return computer
npc_guess()


def complete(player, computer): #determines if the computer beat you, if not you win or tie.
    if computer == ('rock' and player == 'lizard') or ( computer == 'rock' and player == 'paper')\
        or (computer == 'paper' and player == 'scisscors') or (computer == 'paper' and player == 'Spock')\
        or (computer == 'scisscors' and player == 'paper') or (computer == 'paper' and player == 'lizard')\
        or (computer == 'lizard' and player == 'paper') or (computer == 'lizard' and player == 'Spock')\
        or (computer == 'Spock' and player == 'rock') or (computer == 'Spock' and player == 'scisscors'):
            print("The Computer wins")
    elif computer == player:
        print("It was a tie")
    else:
        print("You win!")

main()

有几个问题我想不通。不是我和NPC的选择,它总是说用户是赢家。它还要求我在运行程序时连续两次选择一个选项。你知道吗


Tags: orandtheifcomputerpaperhasplayer
1条回答
网友
1楼 · 发布于 2024-05-16 21:33:45

运行这个,它似乎是工作。请参阅内联注释,以了解关于以前什么不起作用以及更改如何解决问题的解释。你知道吗

还要注意的是,在这段代码中仍然有一些东西需要改进,但我会让您自己去弄清楚。你知道吗

import random

#Rock Paper Scissors Lizard Spock Game

def main():
    print("Let's play rocker, paper, scissors, lizard, Spock")
    player = user_guess() # Switch order of player and computer so player cannot see computer guess before choosing
    computer = npc_guess()
    complete(computer, player)

def user_guess(): #Users hand in RPSLS
    player = input("Choose 'rock', 'paper', 'scissors', 'lizard', 'Spock': ")
    game = True
    while game:
        if player == 'rock' or player == 'paper' or player == 'scissors'\
        or player == 'lizard' or player == 'Spock':
            return player
    else:
        print("That Choice isn't valid.")

def npc_guess(): #computers hand in RPSLS
    while True:
        computer = random.randrange(0, 4)
        if computer == 0:
            print("The NPC has chosen rock")
            computer = 'rock'        # Need string assignment for all these. Had boolean evaluation before. Without assignment, computer was never taking on a value of 'rock', 'paper', 'scissors', 'lizard' or 'Spock', and what was returned was the random integer from above
        elif computer == 1:
            print("The NPC has chosen paper")
            computer = 'paper'
        elif computer == 2:
            print("The NPC has chosen scissors")
            computer = 'scissors'
        elif computer == 3:
            print("The NPC has chosen lizard")
            computer = 'lizard'
        elif computer == 4:
            print("The NPC has chosen Spock")
            computer = 'Spock'
        return computer


def complete(player, computer): #determines if the computer beat you, if not you win or tie.
    if (computer == 'rock' and player == 'lizard') or ( computer == 'rock' and player == 'paper')\ # Previously had if computer == ('rock' and player == 'lizard') or ... , which will always boil down to "if computer == True" since bool('rock') is true. Then "if computer == True" always evaluates to False since computer is never a boolean value
        or (computer == 'paper' and player == 'scissors') or (computer == 'paper' and player == 'Spock')\
        or (computer == 'scissors' and player == 'paper') or (computer == 'paper' and player == 'lizard')\
        or (computer == 'lizard' and player == 'paper') or (computer == 'lizard' and player == 'Spock')\
        or (computer == 'Spock' and player == 'rock') or (computer == 'Spock' and player == 'scissors'):
            print("The Computer wins")
    elif computer == player:
        print("It was a tie")
    else:
        print("You win!")

main()

相关问题 更多 >