简体   繁体   English

Python:我的其他语句中的语法无效

[英]Python: Invalid Syntax in my Else statement

I am starting out with Python and I just cannot figure out what I am doing wrong. 我从Python开始,但我无法弄清楚自己在做什么错。 This is an easy question for you to get a correct answer for! 这是一个简单的问题,可让您获得正确的答案!

Here is my code: 这是我的代码:

def statement(x):
    if x > 50:
        print("X is greater than 50")
    elif x < 50:
        print("X is lower than 50")
    else x == 50:
        print("X is 50")
    return

Very simple I know. 我知道很简单。 When I try and run this, it returns an error saying invalid syntax regarding the X in my else statement: 当我尝试运行此命令时,它返回一条错误,提示我的else语句中有关X的语法无效: Python的屏幕截图

Could anybody please point me in the correct direction as to what I am doing wrong? 有人可以指出我做错了正确的方向吗?

You want elif not else , or you need to remove the condition from the else because else doesn't allow for a conditional statement. 您不需要elif else ,或者您需要从else删除条件,因为else不允许使用条件语句。

if ...
elif x == 50:
    print("X is 50"

or 要么

if ...
else:
    print("X is 50")

else does not take a conditional, like if or elif . else不带条件,例如ifelif If number isn't greater than 50 nor is it lesser than 50, it must be equal to 50: 如果number不大于50也不小于50,则必须等于50:

if x > 50:
    print("X is greater than 50")
elif x < 50:
    print("X is lower than 50")
else: # no condition here!
    print("X is 50")

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

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