如何在Python if语句中正确构造“分数小于10或大于90”的条件?为何`if score < 10 or > 90`会报语法错误?
> 90 Causes a Syntax Error in Your Python Condition Great question—let's break down exactly why that part of your code is throwing a syntax error.
First, let's recall how Python evaluates logical conditions with or:
oris a binary operator, meaning it requires a complete, valid boolean expression on both sides. Each side needs to be something that can resolve toTrueorFalseon its own.
In your code:
if score < 10 or > 90:
The first part (score < 10) is a valid expression—it compares the variable score to 10 and returns a boolean. But the second part (> 90) is incomplete: Python has no idea what value you're trying to compare to 90. Unlike natural language, where you might say "if score is less than 10 or greater than 90" and omit the second "score", Python doesn't infer the missing variable. It expects an explicit left operand for the > operator.
When the Python interpreter reaches > 90, it sees the comparison operator but no value to compare against, so it throws that SyntaxError: invalid syntax—it can't parse an incomplete comparison like that.
To make it clearer, think of each side of or as a separate yes/no question:
- Valid: "Is
scoreless than 10?" (score < 10) - Valid: "Is
scoregreater than 90?" (score > 90) - Invalid: "Is [something] greater than 90?" (Python has no clue what "something" is here)
That's why the error points directly to the > in > 90—the interpreter hits that operator and realizes there's no left value to work with.
内容的提问来源于stack exchange,提问作者Liam Coates

