Skip to content
Advertisement

Looping input till condition is given

I’m a beginner currently working on a small python project. It’s a dice game in which there is player 1 and player 2. Players take turns to roll the dice and a game is 10 rounds in total(5 rounds for each player). The player with the highest number of points wins.

I am trying to validate the first input so that when for example, the player enters a number instead of “Higher” or “Lower”, the program should respond with ‘Invalid input, please respond with “Higher” or “Lower”’.

Here’s my code so far

import random

goAgain = "y"
activePlayer = 1
player1Score = 0
player2Score = 0
maxGuesses = 9
while (goAgain == "y"):
    randNum = random.randint(1, 10)
    print(randNum)
    storedAnswer = input("Will you roll higher or lower?: ")
    while(storedAnswer.strip() != 'higher' or storedAnswer.strip() != 'lower'):
        print('Invalid input, please respond with "Higher" or "Lower"')
        storedAnswer = input("Will you roll higher or lower?: ")
        
    randNum2 = random.randint(1, 10)
    print(randNum2)
    if(storedAnswer.strip() == "lower" and randNum2 < randNum):
        if(activePlayer == 1):
            player1Score+=1
        else:
            player2Score+=1
        print("You Win!")
        maxGuesses+=1
        # print(player1Score)
        # print(player2Score)
    elif (storedAnswer.strip() == "lower" and randNum2 > randNum):
        print("You Lose!")
        maxGuesses+=1
        # print(player1Score)
        # print(player2Score)
    elif (storedAnswer.strip() == "higher" and randNum2 > randNum):
         if(activePlayer == 1):
            player1Score+=1
         else:
            player2Score+=1
         print("You Win!")
         maxGuesses+=1
        # print(player1Score)
        # print(player2Score)
    elif (storedAnswer.strip() == "higher" and randNum2 < randNum):
        print("You Lose!")
        maxGuesses+=1
        # print(player1Score)
        # print(player2Score)
    elif(randNum2 == randNum):
        print("Draw!")
        maxGuesses+=1
        
    print(player1Score)
    print(player2Score)
        
        
    if(activePlayer == 1):
        activePlayer = 2
    else:
        activePlayer = 1
        
    if(maxGuesses == 10):
        if(player1Score > player2Score):
            print("Player 1 wins!")
        elif(player2Score > player1Score):
            print("Player 2 wins!")
        else: 
            print("DRAW!")
        break
    else:
        goAgain = input("Do you want to play again: ")

The problem that occurs is that instead it print ‘invalid input’ regardless of whether the condition is true or not.

Advertisement

Answer

A value is always not equal one or another value.

answer = input("Will you roll higher or lower?: ")
while answer.strip() not in ['higher', 'lower']:
    print('Invalid input, please respond with "Higher" or "Lower"')
    answer = input("Will you roll higher or lower?: ")
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement