没有从程序中获得正确的结果

时间:2015-10-01 11:05:07

标签: python python-3.x

python的新手,试图将我的Python知识用于测试。在插入你的名字后,我正在创建一个程序,让你获得成绩。课程标记&预先标记。该程序没有带出正确的成绩,我不知道为什么......

#Written by Bobby O


#def Get_Details():
Student_Name = input("Enter the name of the student: ")
Coursework_Mark = int(input("Enter the coursework mark achieved by the student: "))
while Coursework_Mark < 0 or Coursework_Mark >60:
   print("Try again, remember the coursework mark is out of 60.")
   Coursework_Mark = int(input("Enter the coursework mark achieved by the student: "))

Prelim_Mark = int(input("Enter the prelim mark achieved by the student: "))
while Prelim_Mark < 0 or Prelim_Mark > 90:
    print("Try again, remember the prelim mark is out of 90.")
    Prelim_Mark = int(input("Enter the prelim mark achieved by the student: "))

#return Student_Name, Coursework_Mark, Prelim_Mark

#def Calculate_Percentage(Coursework_Mark, Prelim_Mark):
   Percentage = ((Coursework_Mark + Prelim_Mark)/150) * 100

if Percentage >= 70:
   Grade = "A"
elif Percentage >= 60 or Percentage <= 69:
   Grade = "B"
elif Percentage >= 50 or Percentage <= 59:
   Grade = "C"
elif Percentage >= 45 or Percentage <= 49:
   Grade = "D"
else:
   Grade = "No Award"

#return Percentage, Grade


#def Display_Results(Student_Name, Grade):
print(Student_Name + " achieved a grade " + Grade)

1 个答案:

答案 0 :(得分:1)

这是因为您在测试边界时使用的是or,而您应该使用and

elif Percentage >= 60 or Percentage <= 69:
   Grade = "B"

在这种情况下,如果Percentage50,则Percentage <= 69将返回True

您不想检查数字是否大于60或小于69.您想要检查是否这两个值,所以您应该这样做:< / p>

elif Percentage >= 60 and Percentage <= 69:
    Grade = "B"

(这适用于所有边界检查)