调用变量时Python中的语法无效

时间:2020-11-28 23:11:22

I'm trying to make the code display Grade B and Grade C when I am giving it a certain exam score and attendance. However, it isn't doing this. The Grade A bit is perfect though. It's coming up with an invalid syntax error for examscore, although I've used the same 'syntax' on the two lines above it and it's not working. I'm only new to Python and this is one of my first projects.

当我给它一定的考试成绩和出勤率时,我正试图让代码显示B级和C级。但是,它没有这样做。 A级比特是完美的。它为examscore提出了一个无效的语法错误,虽然我在它上面的两行上使用了相同的'语法'而且它不起作用。我只是Python的新手,这是我的第一个项目之一。

examscore = int(input(("Enter exam score: ")))
attendance = int(input(("Enter attendance: ")))

if examscore >90 and attendance >90:
     print("Grade A")

elif examscore >80 or <=90 and attendance >90:
     print("Grade B")

elif examscore >70 or <=80 and attendance >90:
     print("Grade C")

This is the error I got.

这是我得到的错误。

SyntaxError: invalid syntax

Thanks in advance.

提前致谢。

5 个解决方案

#1


0  

Actually, you don't need to check the upper range at all since it is always covered by previous checks and you're using elif.

实际上,你根本不需要检查较高的范围,因为它总是被先前的检查覆盖并且你正在使用elif。

examscore = int(input(("Enter exam score: ")))
attendance = int(input(("Enter attendance: ")))

if examscore >90 and attendance >90:
     print("Grade A")

elif examscore >80 and attendance >90:
     print("Grade B")

elif examscore >70 and attendance >90:
     print("Grade C")

#2


0  

You need:

elif examscore >80 or examscore <= 90 and attendance >90:

instead of:

elif examscore >80 or <= 90 and attendance >90:

#3


0  

You cannot write elif examscore >80 or <=90, the correct syntax is elif examscore >80 <=10

你不能写elif examscore> 80或<= 90,正确的语法是elif examscore> 80 <= 10

#4


0  

You have a couple of problems.

你有几个问题。

First off:

examscore >80 or <=90 and attendance >90

is not valid syntax. Think of it as [examscore > 80] OR [ <= 90 ] AND [attendance > 90]. I think you'll see that [ <= 90 ] is not something that can be evaluated. Instead, it should say [examscore <= 90 ].

是无效的语法。可以把它想象成[考试> 80]或[<= 90]和[考勤> 90]。我想你会看到[<= 90]不是可以评估的东西。相反,它应该说[examscore <= 90]。

Secondly, different languages follow different rules in how they "bind" AND and OR conditions. Without parentheses, it's hard to determine how you intend for this logic to be evaluated. I'm guessing that what you're hoping for is:

其次,不同的语言遵循不同的规则来“绑定”AND和OR条件。如果没有括号,很难确定如何评估此逻辑。我猜你所希望的是:

    examscore > 80 or (examscore <=90 and attendance > 90)

meaning that anyone who scored over 80 or less than 90 but had an attendance of greater than 90, gets a "B" (which is a funny metric for getting a B -- they could score 0, and as long as they where in class, they get "B"? -- but that's not relevant to your question). Without the parens, it's hard to be sure.

意味着任何得分超过80或低于90但出勤率超过90的人都会获得“B”(这对于获得B而言是一个有趣的指标 - 他们可以得分为0,并且只要他们在课堂上的位置,他们得到“B”? - 但这与你的问题无关)。没有parens,很难确定。

#5


0  

Your syntax error is due to invalid or condition. <= expects variables on both sides.

您的语法错误是由于无效或条件造成的。 <=期望双方都有变量。

elif examscore >80 or <=90 and attendance >90:  # Error here
     print("Grade B")

elif examscore >70 or <=80 and attendance >90:  # Here as well
     print("Grade C")

You can fix this by using

您可以使用以解决此问题

elif 80 < examscore <= 90 and attendence > 90:

#1


0  

Actually, you don't need to check the upper range at all since it is always covered by previous checks and you're using elif.

实际上,你根本不需要检查较高的范围,因为它总是被先前的检查覆盖并且你正在使用elif。

examscore = int(input(("Enter exam score: ")))
attendance = int(input(("Enter attendance: ")))

if examscore >90 and attendance >90:
     print("Grade A")

elif examscore >80 and attendance >90:
     print("Grade B")

elif examscore >70 and attendance >90:
     print("Grade C")

#2


0  

You need:

elif examscore >80 or examscore <= 90 and attendance >90:

instead of:

elif examscore >80 or <= 90 and attendance >90:

#3


0  

You cannot write elif examscore >80 or <=90, the correct syntax is elif examscore >80 <=10

你不能写elif examscore> 80或<= 90,正确的语法是elif examscore> 80 <= 10

#4


0  

You have a couple of problems.

你有几个问题。

First off:

examscore >80 or <=90 and attendance >90

is not valid syntax. Think of it as [examscore > 80] OR [ <= 90 ] AND [attendance > 90]. I think you'll see that [ <= 90 ] is not something that can be evaluated. Instead, it should say [examscore <= 90 ].

是无效的语法。可以把它想象成[考试> 80]或[<= 90]和[考勤> 90]。我想你会看到[<= 90]不是可以评估的东西。相反,它应该说[examscore <= 90]。

Secondly, different languages follow different rules in how they "bind" AND and OR conditions. Without parentheses, it's hard to determine how you intend for this logic to be evaluated. I'm guessing that what you're hoping for is:

其次,不同的语言遵循不同的规则来“绑定”AND和OR条件。如果没有括号,很难确定如何评估此逻辑。我猜你所希望的是:

    examscore > 80 or (examscore <=90 and attendance > 90)

meaning that anyone who scored over 80 or less than 90 but had an attendance of greater than 90, gets a "B" (which is a funny metric for getting a B -- they could score 0, and as long as they where in class, they get "B"? -- but that's not relevant to your question). Without the parens, it's hard to be sure.

意味着任何得分超过80或低于90但出勤率超过90的人都会获得“B”(这对于获得B而言是一个有趣的指标 - 他们可以得分为0,并且只要他们在课堂上的位置,他们得到“B”? - 但这与你的问题无关)。没有parens,很难确定。

#5


0  

Your syntax error is due to invalid or condition. <= expects variables on both sides.

您的语法错误是由于无效或条件造成的。 <=期望双方都有变量。

elif examscore >80 or <=90 and attendance >90:  # Error here
     print("Grade B")

elif examscore >70 or <=80 and attendance >90:  # Here as well
     print("Grade C")

You can fix this by using

您可以使用以解决此问题

elif 80 < examscore <= 90 and attendence > 90: