0

Im working on a little project for school and we need to set a difficulty between 1 and 3, but when someone enters a wrong number they get a line saying please choose between 1 and 3, but the question should repeat itself, now the code just ends when u enter a wrong number.

difficulty = int(input("Difficulty: "))

while 0 > difficulty > 4:
    print("This is not a valid difficulty, please choose 1, 2 or 3")
    difficulty = int(input("Difficulty: "))

if 0 < difficulty < 4:
    print("The playing board was created with difficulty: " + str(difficulty))
GielConinx
  • 25
  • 2

4 Answers4

1

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))
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
1

Try like this:

difficulty = int(input("Enter input :"))
while difficulty<1 or difficulty>3:
  difficulty = int(input("Enter input between 1 and 3 :"))
print("correct input:",difficulty)
Shiva
  • 476
  • 3
  • 15
1

"While difficulty is under 0 and difficulty is over 4" can never be true, since there's no number that's simultaneously smaller than 0 and larger than 4. The most readable way to format the condition is using a range:

difficulty = int(input("Difficulty: "))

while difficulty not in range(1, 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))

You can also omit the if, since the loop already ensures that the value is within a valid range; no need to check again.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

Try with bit of recursion!

def getDiff():
    difficulty = int(input("Difficulty: "))

    if 0 < difficulty < 4:
        print("The playing board was created with difficulty: " + str(difficulty))
        return 
    else:
        print("This is not a valid difficulty, please choose 1, 2 or 3")
        getDiff()

getDiff()

Edit

Returning difficulty

def getDiff():
    difficulty = int(input("Difficulty: "))

    if 0 < difficulty < 4:
        print("The playing board was created with difficulty: " + str(difficulty))
        return difficulty 
    else:
        print("This is not a valid difficulty, please choose 1, 2 or 3")
        getDiff()

difficulty = getDiff()
Ardein_
  • 166
  • 6
  • This doesn't *return* the difficulty, I'd think OP intends to use that value later on, not just print it. – deceze May 10 '19 at 07:24