简体   繁体   中英

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

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!

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
>>>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM