简体   繁体   English

无法弄清楚为什么我的 if/else 语句不像我想要的那样工作

[英]Can not figure out why my if/else statement doesn`t work like i intend

I am trying to learn simple python.我正在尝试学习简单的python。 Just testing myself with a simple code before moving on.在继续之前,只需用一个简单的代码测试自己。

My goal is for the program to figure out your average grade, and tell you if it is good or not.我的目标是让程序计算出你的平均成绩,并告诉你它是否好。 Very simple.很简单。 Problem occurs when typing using the code, the code will under almost every circumstance say your grade is good.使用代码打字时会出现问题,代码几乎在任何情况下都会说您的成绩很好。 If I enter every grade I have as 2, the average grade would clearly be 2. Yet the program says the grade is good.如果我输入的每个成绩都为 2,那么平均成绩显然是 2。但是程序说成绩很好。

I initially thought it was a problem with the program reading the inputs as strings.我最初认为这是程序将输入读取为字符串的问题。 Therefore I tried clarifying that it is supposed to be read as a float.因此,我试图澄清它应该被视为浮点数。 This very well might still be a problem, as I have maybe not clarified it in the right spot of the program.这很可能仍然是一个问题,因为我可能没有在程序的正确位置澄清它。 I have tried switching between ">=" and "<=" in my if statement.我尝试在 if 语句中的 ">=" 和 "<=" 之间切换。

Im sure the solution is very simple, but I can not figure it out by myself right now.我确定解决方案非常简单,但我现在无法自己弄清楚。 Kind of expecting myself to facepalm at how easy it is.有点期待自己面对它是多么容易。

Anyway, here is the code:无论如何,这是代码:

#Making user type in different grades
math_grade = float(input("Type your math grade here: "))
english_grade = float(input("Type your english grade here: "))
gym_grade = float(input("Type your gym grade here: "))

#Figuring out average grade 
average = math_grade + english_grade + gym_grade / 3

#Attempting to make every grade 3 or under "bad" and everything above "good."
if average <= 3:
    print("Your average grade is bad!")

else:
    print ("Your average grade is good!")

Very little code so should not be hard to copy/paste it and experiment with inputs yourself.很少的代码,所以不应该很难复制/粘贴它并自己尝试输入。 Thank you for whatever help I get :)感谢您为我提供的任何帮助:)

Just a simple issue with the order in which Python completes the mathematical equation.只是 Python 完成数学方程的顺序的一个简单问题。 Just put the additions in brackets when calculating the average.计算平均值时,只需将加法放在括号中。

average = (math_grade + english_grade + gym_grade) / 3

I am trying to learn simple python.我正在尝试学习简单的python。 Just testing myself with a simple code before moving on.在继续之前,仅用一个简单的代码测试自己。

My goal is for the program to figure out your average grade, and tell you if it is good or not.我的目标是让该计划找出您的平均成绩,并告诉您它的好坏。 Very simple.很简单。 Problem occurs when typing using the code, the code will under almost every circumstance say your grade is good.使用代码键入时会出现问题,几乎在每种情况下该代码都会说您的成绩很好。 If I enter every grade I have as 2, the average grade would clearly be 2. Yet the program says the grade is good.如果我输入的每个成绩均为2,则平均成绩显然为2。但是程序显示该成绩很好。

I initially thought it was a problem with the program reading the inputs as strings.最初,我认为程序将输入作为字符串读取是一个问题。 Therefore I tried clarifying that it is supposed to be read as a float.因此,我试图澄清它应该被视为浮点数。 This very well might still be a problem, as I have maybe not clarified it in the right spot of the program.这可能仍然是一个问题,因为我可能没有在程序的正确位置对其进行澄清。 I have tried switching between ">=" and "<=" in my if statement.我曾尝试在if语句中在“> =”和“ <=”之间切换。

Im sure the solution is very simple, but I can not figure it out by myself right now.我确定解决方案非常简单,但我现在无法自己解决。 Kind of expecting myself to facepalm at how easy it is.有点希望自己能轻松面对自己。

Anyway, here is the code:无论如何,这是代码:

#Making user type in different grades
math_grade = float(input("Type your math grade here: "))
english_grade = float(input("Type your english grade here: "))
gym_grade = float(input("Type your gym grade here: "))

#Figuring out average grade 
average = math_grade + english_grade + gym_grade / 3

#Attempting to make every grade 3 or under "bad" and everything above "good."
if average <= 3:
    print("Your average grade is bad!")

else:
    print ("Your average grade is good!")

Very little code so should not be hard to copy/paste it and experiment with inputs yourself.很少的代码,因此应该不难复制/粘贴并自己尝试输入。 Thank you for whatever help I get :)谢谢你给我的帮助:)

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

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