【问题标题】:Why am I getting this message invalid syntax?为什么我收到此消息无效语法?
【发布时间】:2019-12-01 20:25:01
【问题描述】:

我写了代码 if 语句,但它有一个错误。 这是代码

mark = float(input('enter your mark : '))
if mark < 50:
    result = 'failed'
elif mark >= 50 and < 75:
    result = 'accepted'
elif mark >= 75 and < 85:
    result = 'good'
elif mark >= 85 and < 90:
    result = 'very good'
else:
    result = 'excellent'
print(result)

关于

【问题讨论】:

标签: python python-3.x windows if-statement syntax


【解决方案1】:

mark &gt;= 50 and &lt; 75 不是有效的表达式,您必须改写 mark &gt;= 50 and mark &lt; 75。或者,您可以使用链式比较:50 &lt;= mark &lt; 75

【讨论】:

  • elif mark >= 50 和 mark
  • @haithemhussien 很高兴我能帮上忙!如果您觉得我的回答有用,请记得点赞!
  • 你很好,我放弃投票,我是一个像你一样的新贡献者:)
【解决方案2】:

正确的语法是elif mark &gt;= 50 and mark &lt; 75:elif 50 &lt;= mark &lt; 75:

【讨论】:

  • elif mark >= 50 and mark
【解决方案3】:

这将是您实际运行的代码:

mark = float(input('enter your mark : '))
if mark < 50:
    result = 'failed'
elif mark >= 50 and mark < 75:
    result = 'accepted'
elif mark >= 75 and mark < 85:
    result = 'good'
elif mark >= 85 and mark < 90:
    result = 'very good'
else:
    result = 'excellent'
print(result)

正如其他人所说,mark &gt;= 50 and &lt; 85 在 Python 中无效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    相关资源
    最近更新 更多