简体   繁体   中英

how to use an if else statement in another while loop

I am new to coding. I want to try writing a simple rock paper scissors game. But I can't figure out how to end the game.

In the end of this program if the user input is wrong I want to go to the end variable again. I tried with the commented lines but its not working.

player1 = input("What is player 1's name ? ")
player2 = input("What is player 2's name ? ")

player1 = player1.title()
player2 = player2.title()
while True:
    print(player1 + " What do you choose ? rock / paper / scissors : ")
    a = input()

    print(player2 + " What do you choose ? rock / paper / scissors : ")
    b = input()
    if a == "rock" and b == "scissors" :
        print(player1, "won !!!")
    elif a == "scissors" and b == "rock":
        print(player2, "won !!!")

    elif a == "paper" and b == "rock":
        print(player1, "won !!!")
    elif a == "rock" and b == "paper":
        print(player2, "won !!!")

    elif a == "scissors" and b == "paper":
        print(player1, "won !!!")
    elif a == "paper" and b == "scissors":
        print(player2, "won !!!")

    elif a == b:
        print("Its a tie :-(")

    elif a or b != "rock" or "paper" or "scissors":

        print("Wrong input, Try again")
    end = input("Do you want to play again ? yes/no ") == "yes"
    if input == "yes":
        continue
    else:

        print('''

        GAME OVER''')
        break
#    elif input != "yes" or "no":
#        print("Wrong input, Try again. yes or no ?")

I expect it to end game if the input is "no" and restart the game if input is "yes" if the input is not correct I want the prompt to appear again.

Just check the value of end

if end is True:
    continue
else:
    break

Since, you have set the value of end as a boolean by comparing the input() to "yes", it will say whether the user wants to end the game? Also, you are not initializing the input variable, and the last elif condition will always be true as mentioned in the comment.

Your code has a few issues which need some addressing, and a few places where it can be streamlined. I have made a few changes to your program as well as added a few comments explaining the changes.

player1 = input("What is player 1's name ? ").title() #This uses chaining to streamline code 
player2 = input("What is player 2's name ? ").title() #Same as above

while True:
    a = input(player1 + " What do you choose ? rock / paper / scissors : ") #no need to use a separate print statement
    b = input(player2 + " What do you choose ? rock / paper / scissors : ")

    valid_entries = ["rock", "paper", "scissors"] #To check for valid inputs

    if (a not in valid_entries) or (b not in valid_entries):
        print("Wrong input, try again")
        continue

    a_number = valid_entries.index(a) #Converting it to numbers for easier comparison
    b_number = valid_entries.index(b)

    if(a_number == b_number):
         print("Its a tie :-(")
    else:
        a_wins = ((a_number > b_number or (b_number == 2 and a_number == 0)) and not (a_number == 2 and b_number == 0)) #uses some number comparisons to see who wins instead of multiple if/elif checks

        if(a_wins):
            print(player1, "won !!!")
        else:
            print(player2, "won !!!")

    end = input("Do you want to play again ? yes/no ")

    while (end !="yes") and (end != "no"):
        print("invalid input, try again")
        end = input("Do you want to play again ? yes/no ")

    if end == "yes":
        continue
    else:
        print("GAME OVER")
        break

These changes also make the check by using another while loop to see if the input to restart the game was valid or not

*Note that I have not tested these changes and some edits may need to be be made

Well you can simplify your code using a list and then simplify your if tests. You can check the order of the options and based on it make a decision. You can also make the tests standard to minimize the number of if statements. This my suggestion to improve your code. I hope it helps:

# get playe names
player1 = input("What is player 1's name ? ")
player2 = input("What is player 2's name ? ")
player1 = player1.title()
player2 = player2.title()

# init vars
options = ["rock", "paper", "scissors"]
players = [player1, player2]

# start game
while True:
    a = input(player1 + " What do you choose ? rock / paper / scissors : ")
    b = input(player2 + " What do you choose ? rock / paper / scissors : ")

    # check if inputs are correct
    while (a not in options or b not in options):
        print("Wrong input, Try again")
        a = input(player1 + " What do you choose ? rock / paper / scissors : ")
        b = input(player2 + " What do you choose ? rock / paper / scissors : ")

    # check who won
    if abs(options.index(a) - options.index(b)) == 1:
        print(players[1*int(options.index(a) > options.index(b))], "won !!!")

    elif abs(options.index(b) - options.index(a)) > 1:
        print(players[1*int(options.index(a) > options.index(b))], "won !!!")

    elif a == b:
        print("Its a tie :-(")

    # continue or drop game
    end = input("Do you want to play again ? yes/no ")
    if end == "yes":
        continue
    else:

        print('''

        GAME OVER''')
        break

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