0

My while loop just keeps on going even though I input y, yes, n, or no.

import random

def rollDice():
    diceRoll = random.randint(1,6)
    return diceRoll
    
def reRollDie1():
    reRollDie1 = input("Would you like to re roll die 1?")
    reRollDie1.lower()
    
    while reRollDie1 != "yes" or reRollDie1 != "y" or reRollDie1 != "no" or reRollDie1 != "n":
        reRollDie1 = input("Sorry that answer is invalid please try again. Would you like to re-roll die 1? ")         
reRollDie1()

OUTPUT:

Sorry that answer is invalid please try again. Would you like to re-roll die 1? no

Sorry that answer is invalid please try again. Would you like to re-roll die 1? yes

Sorry that answer is invalid please try again. Would you like to re-roll die 1? yes

Community
  • 1
  • 1
numnah
  • 21
  • 2
  • Consolidating your multiple inequalities to an `in` can help you better understand what's going on. – FatihAkici Nov 20 '19 at 18:05
  • I think you just want `while reRollDie1 in ('yes', 'y'):`. That way, the condition will be false if the user enters `n` or `no`. – Sumner Evans Nov 20 '19 at 18:06
  • 1
    Thats not true @SumnerEvans OP wants to check for valid input it can `yes`, `y`, `n` or `no`. – Poojan Nov 20 '19 at 18:08

1 Answers1

3

You're checking all the options with or. Since each is an inequality test, and each option is different from the others, the condition will always be true. I would instead suggest a test such as reRollDie1 not in {"yes", "y", "no", "n"}.

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26
  • Thanks. I don't know if this is any better but I just added an if statement inside my while loop. – numnah Nov 20 '19 at 18:10
  • if reRollDie1 == "yes" or reRollDie1 == "y": rollDie1 = rollDice() break – numnah Nov 20 '19 at 18:10
  • 1
    There's an important difference between this test and the one in your question. Equality tests with `==` can be meaningfully combined with `or`, but the inequality tests with `!=` would need `and` instead. Bear in mind your variable has the same value in each subtest. This relates to [de Morgan's laws](https://en.wikipedia.org/wiki/De_Morgan%27s_laws) in logic. – Yann Vernier Nov 20 '19 at 18:13