1

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

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Arjuna
  • 188
  • 1
  • 11
  • 1
    `if Sens not in ['0', '1', '2', '3', '4', '5']:` – It_is_Chris Oct 28 '21 at 18:16
  • @It_is_Chris Why a list of strings instead of a set of ints? – wjandrea Oct 28 '21 at 18:17
  • @wjandrea because of the input: `Sens = input("""Sensitivity (MIN SENS 0, 1, 2, 3, 4, 5 MAX SENS): """)` the OP is not converting it to an int: `Sens = int(input("""Sensitivity (MIN SENS 0, 1, 2, 3, 4, 5 MAX SENS): """))` – It_is_Chris Oct 28 '21 at 18:18
  • @It_is_Chris So convert it... – wjandrea Oct 28 '21 at 18:19
  • @wjandrea The OP can if he/she/they want to. – It_is_Chris Oct 28 '21 at 18:21
  • Thanks so much for the help everyone!, I have found the answer and accepted it. never expected so many people to actually be nice and help and explain it, have an awesome day everyone! (especially want to thank: AziMez for the answer, @wjandrea for the edits, spelling improvements, and help in the comments and It_is_Chris for also helping everyone in the comments, you guys are awesome!) – Arjuna Oct 28 '21 at 18:43

1 Answers1

3

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):
wjandrea
  • 28,235
  • 9
  • 60
  • 81
AziMez
  • 2,014
  • 1
  • 6
  • 16
  • Couldn't you use a try and except call instead of the weird one line ```if not``` statement – Brody Critchlow Oct 28 '21 at 18:21
  • nit: `if Sens >=6 or Sens < 0` would be more optimal than `not in range(6)` – OneCricketeer Oct 28 '21 at 18:30
  • @BrodyCritchlow I'm no python pro, but expecting an error and working off that if you can also just fix the code seems weird – Arjuna Oct 28 '21 at 18:31
  • 1
    @OneCricketeer Actually, [those are equivalent in Python 3](/a/30081318/4518341)! Provided that `Sens` is an integer, of course. – wjandrea Oct 28 '21 at 18:41
  • @Frostbite You're right, try-except wouldn't really make sense in this case. I'm not even sure what error Brody would expect to catch. – wjandrea Oct 28 '21 at 18:43