简化Python代码(Rock,Paper,剪刀)

时间:2018-07-22 17:39:20

标签: python simplify

我是一个完整的python新手,这是我关于stackoverflow的第一个问题,所以请耐心等待我:)

因此,为了获得一些帮助,我尝试使用Python编写自己的石头,纸,剪刀游戏。但是,与其他剪刀石头布,纸张,剪刀程序相比,我的代码相对较长。这是因为我编写了游戏中每个可能的选项。有可能简化此代码吗?就像不必对游戏中的每一个可能性进行编程一样?因为这样做可以在剪刀,布,剪刀上实现,但在更高级的问题中可能无法实现。

让我知道您的想法,谢谢!!

祝一切顺利, 卢卡·韦斯贝克(Luca Weissbeck)

代码:

#Rock, Paper, Scissors
while True:
    Game_list = ["Rock", "Paper", "Scissors"]
        User_1 = str(input("Rock, Paper, Scissors?"))
    #Let the computer make its choice
        import random
        Computer_1 = random.choice(Game_list)
    #Possibility of a draw
        if str(Computer_1) == User_1:
            Draw_choice = str(input("It's a draw. Do you want to replay?(Y/N)"))
            if Draw_choice == "Y":
                continue
            else:
                break
    #Possibility of player winning
        if str(Computer_1) == "Rock" and User_1 == "Paper" or str(Computer_1) == 
    "Paper" and User_1 == "Scissors" or str(Computer_1) == "Scissors" and User_1 
    == "Rock":
            UW1 = str(input("You won. The computer chose:" + Computer_1 + " Do 
    you want to play again? (Y/N)"))
            if UW1 == "Y":
                continue
            else:
            break
    #Possibility of computer winning
        if str(Computer_1) == "Rock" and User_1 == "Scissors" or str(Computer_1) 
    == "Paper" and User_1 == "Rock" or str(Computer_1) == "Scissors" and User_1 
    == "Paper":
            UL1 = str(input("You lost. The Compuer chose:" + Computer_1 + " Do 
    you want to play again? (Y/N)"))
            if UL1 == "Y":
                continue
            else:
                break
    #End sentence                  
    print("Bye, thank you for playing!")

3 个答案:

答案 0 :(得分:1)

此程序中有很多重复的字符串。它们可以折叠。

import random
States = ['Rock','Paper','Scissors']
playAgain = True
while playAgain:
    User = str(input("Choose your play: "))
    try:
        User = States.index(User)
    except: 
        print('Your choice is not one of the choices in Rock, Paper, Scissors')
        break
    Comp = random.randint(0,2)
    winner = (Comp-User)%3
    if winner==0:
        print("There is a tie. Both User and Computer chose " + States[User])
    elif winner==1:
        print("Computer wins. Computer chose "+States[Comp]+" and User chose "+States[User])
    else:
        print("User wins. Computer chose "+States[Comp]+" and User chose "+States[User])
    if str(input("Do you want to play again? (Y/N)")) == "N":
        playAgain = False
print("Thanks for playing!")

答案 1 :(得分:0)

您可以尝试存储获胜的可能性。

win_case = [['rock','scissor'], ['scissor','paper'], ['paper','rock']]

然后主程序会像

if (user == comp):
     // draw
elif ([user,comp] in win_case):
     // user win
else:
     // comp win

答案 2 :(得分:0)

代码长度是一回事。组织和可读性是另一个(更重要的)特征。通常会帮助分离代码和配置。首先设置常量,设置,消息,然后在代码中使用它们:

import random

# game config
R, P, S = "Rock", "Paper", "Scissors"
BEATS = {R: S, P: R, S: P}
MESSAGES = {
    "draw": "It's a draw. Play again? (Y/N)\n",
    "win": "You won. Comp chose: {}. Play again? (Y/N)\n",
    "loss": "You lost. Comp chose: {}. Play again? (Y/N)\n",
    "invalid": "Invalid input: {}. Play again? (Y/N)\n",
}

# game code
play = "Y"
while play.upper() == "Y":
    u1 = str(input("{}, {}, {}?\n".format(R, P, S)))
    c1 = random.choice([R, P, S])

    if u1 not in BEATS:
        play = input(MESSAGES["invalid"].format(u1))
    elif c1 == u1:
        play = input(MESSAGES["draw"])
    elif BEATS[u1] == c1:
        play = input(MESSAGES["win"].format(c1))               
    else:
        play = input(MESSAGES["loss"].format(c1))