将用户输入与随机选择的列表项进行比较 - Python

时间:2016-07-28 14:37:50

标签: python

我正在为一堂课创作一个Rock,Paper,Scissors游戏。作为游戏的一部分,我需要在屏幕上显示武器菜单供用户选择。然后计算机将从列表中随机选择一个武器。我面临的问题(我相信)是列表项的范围从[0,2]我的菜单项列表[1,3]。我已经搜索了好几个小时,但我不了解我在线阅读的复杂内容,所以我不确定如何应用它们。

# random integer
from random import randint

# list for weapon
WEAPON = ["Rock", "Paper", "Scissors"]

# one player mode
def onePlayer():
scoreP = 0
scoreC = 0
again = ""
player = False

print("---------------------------------------------")
print("\n\tPlayer VS Computer")

while player == False:
    print("Weapons:")
    print("1. Rock")
    print("2. Paper")
    print("3. Scissors")
    print("4. Quit")

    player = input("\nSelect your weapon: ")
    if player == "quit" or player == "q" or player == "4":
        player = True
        main()
    else:
        try:
            player = int(player)
            if player == 1:
                player = WEAPON[0]
            elif player == 2:
                player = WEAPON[1]
            elif player == 3:
                player = WEAPON[2]
        except:
            print("please enter a number 1 through 4\n")

    computer = WEAPON[randint(0,2)]

    if player == computer:
        print(player," vs ",computer)
        print("It's a tie!\n")
        print("Player:",scoreP,"\nComputer:",scoreC)
        print("")
    elif player == 1:
        # computer == paper
        if computer == 1:
            print(player," vs ",computer)
            print("Paper covers rock! You lose!\n")
            scoreC = scoreC + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
        else:
            print("Rock smashes scissors. You win!\n")
            scoreP = scoreP + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
    elif player == 2:
        if computer == 2:
            print(player," vs ",computer)
            print("Scissors cut paper! You lose!\n")
            scoreC = scoreC + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
        else:
            print("Paper covers rock. You win!\n")
            scoreP = scoreP + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
    elif player == 3:
        if computer == 0:
            print(player," vs ",computer)
            print("Rock smashes scissors! You lose!\n")
            scoreC = scoreC + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
        else:
            print("Scissors cut paper. You win!\n")
            scoreP = scoreP + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
    #else:
    #    print("Please select a valid play option\n")
    player = False       

请不要介意if / else语句中的print语句。我意识到这些都需要改变。我的主要问题是将用户输入与计算机的随机列表选择进行比较的逻辑。

4 个答案:

答案 0 :(得分:3)

您需要注意变量的内容:

# this is storing a string     
computer = WEAPON[randint(0,2)]

# this expects an integer
elif player == 1:
        # computer == paper
        if computer == 1:

这将是您所看到的一些问题的根源。

此外,通常,当编码尝试使用有意义的变量名称并避免重复使用它们的目的不止一个:在这种情况下,两个新的变量,如player_weapon和computer_weapon(而不是重用播放器和计算机)可能会阻止你错误。声明变量时不要懒惰! ;)

答案 1 :(得分:1)

在if语句中,您似乎将变量computer(一个字符串)与整数进行比较。您指定computer = WEAPON[randint(0,2)],因此计算机是以下之一:["Rock", "Paper", "Scissors"]。但是,在if语句中,您要说:if computer == 1:将其与人进行比较(您的person变量具有相同的方式;在将其与整数进行比较之前为其指定一个字符串)。

你必须确保你正在比较苹果和苹果

答案 2 :(得分:1)

比较字符串,而不是数字,比如

    if player == computer:
        print(player," vs ",computer)
        print("It's a tie!\n")
        print("Player:",scoreP,"\nComputer:",scoreC)
        print("")
    elif player == 'Rock':
        # computer == paper
        if computer == 'Paper':
            print(player," vs ",computer)
            print("Paper covers rock! You lose!\n")
            scoreC = scoreC + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
        else:
            print("Rock smashes scissors. You win!\n")
            scoreP = scoreP + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
    elif player == 'Paper':
        if computer == 'Scissors':
            print(player," vs ",computer)
            print("Scissors cut paper! You lose!\n")
            scoreC = scoreC + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
        else:
            print("Paper covers rock. You win!\n")
            scoreP = scoreP + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
    elif player == 'Scissors':
        if computer == 'Rock':
            print(player," vs ",computer)
            print("Rock smashes scissors! You lose!\n")
            scoreC = scoreC + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
        else:
            print("Scissors cut paper. You win!\n")
            scoreP = scoreP + 1
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")
    #else:
    #    print("Please select a valid play option\n")
    player = False

答案 3 :(得分:1)

我通过实现一个小的dict_map来压缩大部分代码。它可以进一步浓缩,但为什么要这么麻烦。

# random integer
from random import randint

# list for weapon
WEAPON = ["Rock", "Paper", "Scissors"]
MAPP = {"Rock":{"Win":'Scissors', "Loss":"Paper", "Adj":"Smashes"},
        "Paper":{"Win":"Rock", "Loss":"Scissors", "Adj":"Covers"},
        "Scissors":{"Win":"Paper", "Loss":"Rock", "Adj":'Cuts'}}

def have_won(player, computer):
    #determines if the players choice has beaten the computers
    if MAPP[player]["Win"] == computer:
        adj = MAPP[player]['Adj']
        return True, ' '.join([player, adj, computer])
    else:
        adj = MAPP[computer]['Adj']
        return False, ' '.join([computer, adj, player])

# one player mode
def onePlayer():
    scoreP = 0
    scoreC = 0
    again = ""
    player = False

    print("---------------------------------------------")
    print("\n\tPlayer VS Computer")

    while player == False:
        print("Weapons:")
        print("1. Rock")
        print("2. Paper")
        print("3. Scissors")
        print("4. Quit")

        player = input("\nSelect your weapon: ")
        if player == "quit" or player == "q" or player == "4":
            player = True
        else:
            try:
                player = int(player)
                if player == 1:
                    player = WEAPON[0]
                elif player == 2:
                    player = WEAPON[1]
                elif player == 3:
                    player = WEAPON[2]
            except:
                print("please enter a number 1 through 4\n")

        computer = WEAPON[randint(0,2)]
        print player, computer
        outcome = have_won(player, computer)
        if player == computer:
            print(player," vs ",computer)
            print("It's a tie!\n")
            print("Player:",scoreP,"\nComputer:",scoreC)
            print("")

        elif outcome[0] == True:
            print(outcome[1]+"! You Win!!")
            scoreP += 1
        elif outcome[0] == False:
            print(outcome[1]+"! You Lose!!")
            scoreC += 1
        #else:
        #    print("Please select a valid play option\n")
        print("Player:",scoreP,"\nComputer:",scoreC)
        player = False
onePlayer()