What is wrong with my code? I am a newbie and I don't know why it isn't working. (Python)

Question:

Sorry, I’ve only been using python for about an hour, I’m using PyCharm if that has to do with the problem, I don’t think it does though.

Here is my code:

userAge = input("Hi, how old are you?n")

longRussiaString = (
    "When will you guys stop telling me about how you had to walk uphill both ways for 10 miles to"
    "get to school amidst the icy tundra of Russia? We get it!")


def reply_detect():
    if userAge != 0 - 5:
        pass
    else:
        print("Wait a minute... Stop lying! I know your brain is too small for this!")

    if userAge != 6 - 18:
        pass
    else:
        print("You're too young to be interested in this. Unless your dad forced you to just like me :(")

    if userAge != 19 - 24:
        pass
    else:
        print("""Good luck dealing with those "taxes" things or whatever. Wait, you haven't heard of those?""")

    if userAge != 25 - 40:
        pass
    else:
        print("You post-millennial scumbags... No, just kidding, you guys are the ones carrying our society.")

    if userAge != 41 - 55:
        pass
    else:
        print(longRussiaString)


def age_reply():
    print(f"So, you're {userAge} years old, huh?")
    reply_detect()


age_reply()

I tried to inverse the if loops making a second function to neaten things up a bit, and lots of other things, what happens is that it shows the "So, you’re {userAge} years old part, but it ends there and doesn’t show me the rest, which is the function "reply_detect".

Thanks!

Asked By: CloudieSki

||

Answers:

you are using a test of 0 - 5 but to python this means zero minus five which is negative five, just as you would do in a calculator. Instead you mean if userAge >= 0 and userAge <= 5 and so on for the remaining tests.

Answered By: topsail

It seems you’re looking to see if a number occurs in a range. This can be accomplished very directly in Python. Just remember that the end of the range is exclusive: 0 to 5 is covered by range(0, 6).

>>> n = 16
>>> n in range(0, 17)
True
>>> n in range(0, 6)
False
>>> n not in range(0, 6)
True
>>>
Answered By: Chris
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.