简体   繁体   中英

I want the user to return to while loop after game finishes

Take input from user 1

player_1 = input("Enter your choice player_1 from rock,paper or scissor: ")

Take input from player 2

player_2 = input("Enter your choice player_2 from rock,paper or scissor: ")

Rules of rock paper scissor 1- paper covers rock 2- rock beats scissors 3- scissors cut paper

answer = []
while True:
    if player_1 == player_2:
        print("The game is tied sorry!!")
    elif player_1 == "paper" and player_2 == "scissor":
        print("scissor beats paper player_2 won")
    elif player_1 == "scissor" and player_2 == "paper":
        print("scissor beats paper player_1 won")
    elif player_1 == "rock" and player_2 == "paper":
        print("paper covers rock player_2 won")
    elif player_1 == "paper" and player_2 == "rock":
        print("paper covers rock player_1 won")
    elif player_1 == "scissor" and player_2 == "rock":
        print("rock breaks scissor player_2 won")
    elif player_1 == "rock" and player_2 == "scissor":
        print("rock breaks scissor player_1 won")

    answer = input("Do you want to play the game again? y or n : ")
    if answer == "y":
        continue 
    else:
        break
answer = []
black5 = True
while black5:

    player_1 = input("Enter your choice player_1 from rock,paper or scissor: ")
    player_2 = input("Enter your choice player_2 from rock,paper or scissor: ")

    if player_1 == player_2:
        print("The game is tied sorry!!")
    elif player_1 == "paper" and player_2 == "scissor":
        print("scissor beats paper player_2 won")
    elif player_1 == "scissor" and player_2 == "paper":
        print("scissor beats paper player_1 won")
    elif player_1 == "rock" and player_2 == "paper":
        print("paper covers rock player_2 won")
    elif player_1 == "paper" and player_2 == "rock":
        print("paper covers rock player_1 won")
    elif player_1 == "scissor" and player_2 == "rock":
        print("rock breaks scissor player_2 won")
    elif player_1 == "rock" and player_2 == "scissor":
        print("rock breaks scissor player_1 won")

    answer = input("Do you want to play the game again? y or n : ")
    if answer != "y":black = False

Will continue if y is choosen adn exit if n is choosen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM