简体   繁体   English

我的代码有什么问题? 我是新手,我不知道为什么它不起作用。 (Python)

[英]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.抱歉,我只使用 python 大约一个小时,如果这与问题有关,我正在使用 PyCharm,但我不认为它确实如此。

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".我试图反转 if 循环,制作第二个 function 来整理一下,还有很多其他事情,发生的事情是它显示“所以,你是 {userAge} 岁的部分,但它在那里结束并且没有给我看看 rest,这是 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) .这可以在 Python 中非常直接地完成。请记住范围的末尾是唯一的: range(0, 6)涵盖05

>>> 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.您正在使用0 - 5的测试,但对于 python 这意味着零减五即负五,就像您在计算器中所做的那样。 Instead you mean if userAge >= 0 and userAge <= 5 and so on for the remaining tests.相反,您的意思是如果userAge >= 0 and userAge <= 5等等,对于剩余的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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