Python while loop breaks in every case

114 Views Asked by At

I'm trying to make a program that rolls a dice and checks if the user wants to continue every roll, if not, the program should halt. Although, no matter what you input, the program breaks out of the loop. Can someone explain why and give me some tips to making a program that is simpler and works? Thanks

import random
sideNumber = int(input("Enter the number of sides in the die: "))
print("Dice numbers: ")

while True:
 print(random.randint(0, sideNumber))
 print("Do you want to continue?")
 response = input()
 if response == "n" or "no":
  break
2

There are 2 best solutions below

0
iFlo On

That's because the statement "no" is still true.

You should do :

if response == "n" or response == "no":

or better :

if response in ["n", "no"] : 
0
kmaork On
if response == "n" or "no":

makes your code fail. This checks if the boolean value of "no" is true, and it always is. replace it with:

if response == "n" or response == "no":