The while loop 0 > difficulty > 4
never executes, since that condition always evaluates to False
, as 0 > 4
is False
, hence I would restructure the while loop as while difficulty > 4 or difficulty < 0:
, which checks if difficulty is less than 0, or greater than 4, also as @deceze pointed out, if
is not needed since the condition only hits when we have made sure that our difficulty is between 0 and 4, excluding 0 and 4
So the answer changes to
difficulty = int(input("Difficulty: "))
#Check if difficulty is less than 0, or greater than 4
while difficulty < 0 or difficulty > 4:
print("This is not a valid difficulty, please choose 1, 2 or 3")
difficulty = int(input("Difficulty: "))
print("The playing board was created with difficulty: " + str(difficulty))
The output will look like
Difficulty: -1
This is not a valid difficulty, please choose 1, 2 or 3
Difficulty: 5
This is not a valid difficulty, please choose 1, 2 or 3
Difficulty: 2
The playing board was created with difficulty: 2
Another way of writing the while loop is, we need to ensure that if the input is less than 0, or bigger than 4, we want to keep running the loop, which can actually be achieved by while not 0 < difficulty < 4:
Then the answer will be changed to
difficulty = int(input("Difficulty: "))
#Check if difficulty is less than 0, or greater than 4
while not 0 < difficulty < 4:
print("This is not a valid difficulty, please choose 1, 2 or 3")
difficulty = int(input("Difficulty: "))
print("The playing board was created with difficulty: " + str(difficulty))