简化 Python 代码(石头剪刀布)

Simplify Python code (Rock, Paper, Scissors)

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

因此,为了获得一些练习,我尝试在 Python 中编写我自己的剪刀石头布游戏。但是,与其他剪刀石头布程序相比,我的代码相对较长。这是因为我对游戏中的每一个可能选项进行了编程。有没有可能简化这段代码?就像不必对游戏中的每一个可能性进行编程一样?因为这样做可能在剪刀石头布中可行,但在更高级的问题中可能不行。

告诉我你的想法,谢谢!!!

一切顺利, 卢卡·魏斯贝克

代码:

#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!")

这个程序中有很多重复的字符串。它们可以折叠起来。

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!")

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

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

那么主程序就是这样

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

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

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