0

logically this makes sense but does not work.

dice = 0
while True:
    dice = int(input("enter dice: "))
    if (dice != 4 or dice != 6 or dice != 8 or dice != 10 or dice != 12 or dice != 20):
        print("invalid")
    elif (dice == 4 or dice == 6 or dice == 8 or dice == 10 or dice == 12 or dice == 20):
        print("valid")
        break
    
print("Accepted dice")
   

I keep getting the invalid print statement. Want to have it as a loop until one of the correct die is selected.

2
  • 1
    The logic in your first if is flawed, it will always evaluate as true. Take dice != 4 or dice != 6, it can never be false, the first part will only be false if dice == 4 but that makes the second part true Commented Oct 13, 2021 at 3:05
  • See also en.wikipedia.org/wiki/De_Morgan%27s_laws Commented Oct 13, 2021 at 3:08

3 Answers 3

1

use a flag, as mentioned in the comment your original first if is flawed ... if you just want to fix it then you can change your or's to and's

dice_is_valid = dice in {4,6,8,10,20}
if dice_is_valid:
  ...
else:
  print("invalid")
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is with using an or statement here. Take for example an input of 4. 4 != 4 if obviously false, but then it evaluates 4 != 6 which is true. No matter what you input that first condition will always be true because of this. Instead just use an else statement like so

dice = 0
while True:
    dice = int(input("enter dice: "))
        print("invalid")
    if (dice == 4 or dice == 6 or dice == 8 or dice == 10 or dice == 12 or dice == 20):
        print("valid")
        break
    else:
        print("invalid")
    
print("Accepted dice")

Comments

0
dice_var = 0
a = [4,6,8,10,12,20]
while True:
    dice_var = int(input("Enter the dice value ;"))
    if dice_var in a:
        print("Valid Value !!!!")
        break
    else:
        print("Invalid Value !!!!")
print("Accepted Dice")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.