Как я могу оптимизировать операторы цикла и условия? - PullRequest
0 голосов
/ 30 января 2019
winner = False

def player():
    global winner
    while winner==False:

        print("player 1: please choose rock/paper/scissor !")
        choice1 = input()
        print("player 2: please choose rock/paper/scissor !")        
        choice2 = input()

        if choice1!=choice2:
            if choice1=="rock" and choice2=="scissor" :
                print("player 1 is the winner, Congrats!!")

            elif choice1=="rock" and choice2=="paper" :
                print("player 2 is the winner, Congrats!!")

            elif choice2=="rock" and choice1=="scissor" :
                print("player 2 is the winner, Congrats!!")

            elif choice2=="rock" and choice1=="paper" :
                print("player 1 is the winner, Congrats!!")

            elif choice1=="scissor" and choice2=="paper" :
                print("player 1 is the winner, Congrats!!")

            elif choice1=="paper" and choice2=="scissor" :
                print("player 2 is the winner, Congrats!!")
        else:
            print("its a draw")


        print("do you want to start a new game?,yes or no")
        answer = input()
        if answer=="yes":
            print("lets start another game!")
            winner = False
        elif answer=="no":
            print("see you next time!")
            winner = True


player()

Как вы можете видеть, в моем коде слишком много неэффективных операторов, как я могу минимизировать их, если это возможно

Ответы [ 2 ]

0 голосов
/ 30 января 2019
def player():
    while True:

        print("player 1: please choose rock/paper/scissor !")
        choice1 = input()
        print("player 2: please choose rock/paper/scissor !")
        choice2 = input()

        beats = dict(
            rock='scissor',
            scissor='paper',
            paper='rock',
        )

        if choice1 != choice2:
            if beats[choice1] == choice2:
                winner = 1
            else:
                winner = 2
            print("player %s is the winner, Congrats!!" % winner)
        else:
            print("its a draw")

        print("do you want to start a new game?,yes or no")
        answer = input()
        if answer == "yes":
            print("lets start another game!")
        else:
            print("see you next time!")
            break


player()
0 голосов
/ 30 января 2019

Вы можете сделать что-то вроде этого:

db = {'rockscissor': 1, 
 'rockpaper': 2, 
 'scissorrock': 2, 
 'scissorpaper': 1,
 'paperrock': 1, 
 'paperscissor': 2}

if choice1!=choice2:
    print("player {} is the winner, Congrats!!".format(db[choice1+choice2]
else:
    print("its a draw")

, и вы даже можете добавить ничьи к db:

db = {'rockscissor': 1, 
 'rockpaper': 2, 
 'scissorrock': 2, 
 'scissorpaper': 1,
 'paperrock': 1, 
 'paperscissor': 2,
 'paperpaper':0,
 'rockrock':0,
 'scissorscissor':0
}

и обработать if.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...