Why won't Python integer input check correctly?

Viewed 73

This is my code:

Sens = input("""Sensitivity (MIN SENS 0, 1, 2, 3, 4, 5 MAX SENS): """)
if not Sens == 0 or Sens == 1 or Sens == 2 or Sens == 3 or Sens == 4 or Sens == 5:
    print("Invalid: [{}]".format(Sens))
    print("Defaulting to 1")

It keeps outputting "Invalid" even though I type 1, 2, 3, 4 or 5 (it does work when i type 0).
Maybe this is really stupid but what am I doing wrong?

I have tried: making everything strings (input and checks), making everything int (input and checks), I have even tried making the input like this: str(int(input = ...)), also didn't work

1 Answers

Is this what you are looking for?

  1. Adding int() to convert input from str to int

  2. Adding parentheses to the logic phrase.

Sens = int(input("""Sensitivity (MIN SENS 0, 1, 2, 3, 4, 5 MAX SENS): """))
if not (Sens == 0 or Sens == 1 or Sens == 2 or Sens == 3 or Sens == 4 or Sens == 5):
    print("Invalid: [{}]".format(Sens))
    print("Defaulting to 1")

As well, you can replace this long line:

if not (Sens == 0 or Sens == 1 or Sens == 2 or Sens == 3 or Sens == 4 or Sens == 5):

by this one:

if Sens not in range(6):
Related