0

I have run into an issue that totally baffles me. Everything I feed into this program comes back with a value of -2:

def score_mod(score): #This is meant to come up with the adjustment for a given ability score
    if score == 2 or 3:
        mod = -3
    if score == 4 or 5:
        mod = -2
    if score == 6 or 7 or 8:
        mod == -1
    if score == 9 or 10 or 11 or 12:
        mod == 0
    if score == 13 or 14 or 15:
        mod == 1
    if score == 16 or 17:
        mod == 2
    if score == 18:
        mod == 3
    return mod
while True:
    feed = input("Number")
    print(score_mod(feed))
    again = input("Again? Y/N")
    again = again.capitalize()
    if again == "Y":
        continue
    if again == "N":
        break
David Parks
  • 30,789
  • 47
  • 185
  • 328
MrZJunior
  • 65
  • 4
  • `score == 2 or 3` is always true, no matter what value `score` has, because if `score == 2` is True, it returns `True`; if `score == 2` is False, `False or 3` evaluates to 3, which interpreted as a boolean is _also_ True. – Charles Duffy Feb 24 '22 at 02:59

1 Answers1

2

Your problem is with if score == 2 or 3.

You can re-write that as (score == 2) or (3) which resolves to False or True or True or True, which is always True.

This is a bit quirky of Python. bool(any_object) returns True if the object is not None. Try bool(3) and see that you get True from it.

What you meant to do is if (score == 2) or (score == 3) or if score in [2, 3], either of those would work.

David Parks
  • 30,789
  • 47
  • 185
  • 328
  • 1
    In [How to Answer](https://stackoverflow.com/help/how-to-answer), note the section _Answer Well-Asked Questions_, and therein the bullet point regarding questions that "have been asked and answered many times before". – Charles Duffy Feb 24 '22 at 03:00
  • Just want to add to this post, the reason that you got ```-2``` specifically is because in all the other if statements below where you did ```_ or _```, you did ```mod == _``` instead of ```mod = _```. Remember that ```==``` checks for equality, and ```=``` assigns! – Aniketh Malyala Feb 24 '22 at 03:02