【发布时间】:2022-05-07 20:04:55
【问题描述】:
问题:编写一个程序,不断地询问用户一个考试分数,该分数以 0 到 100 范围内的整数百分比形式给出。如果输入了除 -1 以外的范围之外的值,则打印出错误并提示用户再试一次。计算输入的所有有效成绩的平均值以及每个字母等级类别中的总成绩如下:90 到 100 是 A,80 到 89 是 B,70 到 79 是 C,60 到 69 是D,而 0 到 59 是 F。使用负分作为标记值来指示输入的结束。 (负值仅用于结束循环,所以不要在计算中使用它。)例如,如果输入是。
#Enter in the 4 exam scores
g1=int(input("Enter an exam score between 0 and 100 or -1 to end: "))
g2=int(input("Enter an exam score between 0 and 100 or -1 to end: "))
g3=int(input("Enter an exam score between 0 and 100 or -1 to end: "))
g4=int(input("Enter an exam score between 0 and 100 or -1 to end: "))
total =(g1 + g2 + g3 + g4)
while g1 is range(0,100):
continue
else:
print("Sorry",g1,"is not in the range of 0 and 100 or -1. Try again!")
while g2 is range(0,100):
continue
else:
print("Sorry",g2,"is not in the range of 0 and 100 or -1. Try again!")
while g3 is range(0,100):
continue
else:
print("Sorry",g3,"is not in the range of 0 and 100 or -1. Try again!")
while g4 is range(0,100):
continue
else:
print("Sorry",g4,"is not in the range of 0 and 100 or -1. Try again!")
#calculating Average
def calc_average(total):
return total/4
def determine_letter_grade(grade):
if 90 <= grade <= 100:
1 + TotalA
elif 80 <= grade <= 89:
1 + TotalB
elif 70 <= grade <= 79:
1 + TotalC
elif 60 <= grade <= 69:
1 + TotalD
else:
1 + TotalF
grade=total
average=calc_average
#printing the average of the 4 scores
print("You entered four valid exam scores with an average of: " + str(average))
print("------------------------------------------------------------------------")
print("Grade Distribution:")
print("Number of A's: ",TotalA)
print("Number of B's: ",TotalB)
print("Number of C's: ",TotalC)
print("Number of D's: ",TotalD)
print("Number of F's: ",TotalF)
给我的示例输出:
Enter an exam score between 0 and 100 or -1 to end: 88.64
Enter an exam score between 0 and 100 or -1 to end: 103
Sorry, 103 is not in the range of 0 and 100 or -1. Try Again!
Enter an exam score between 0 and 100 or -1 to end: 99.10
Enter an exam score between 0 and 100 or -1 to end: 71.52
Enter an exam score between 0 and 100 or -1 to end: 73
Enter an exam score between 0 and 100 or -1 to end: -1
You entered 4 valid exam scores with an average of 83.07.
Grade Distribution
Number of A’s = 1
Number of B’s = 1
Number of C’s = 2
Number of D’s = 0
Number of F’s = 0
注意:这是我的第一堂计算机科学课,所以我确信有一个明显的解决方法是我想念的,但我很感激我能得到的任何帮助
【问题讨论】:
-
只有4个考试成绩吗?或者任意数量的考试成绩?
-
很抱歉,您的解决方案存在很多问题。看来您还没有完全理解这个问题或如何为它编写 Python 解决方案 - 首先,您的解决方案假定应该始终有 4 个输入,但如果只有 3 个呢?还是5?试着想象一下没有代码的任务,想象有人问你分数,把它们加起来,直到你告诉他们你完成了,然后计算结果。如果您以微小的步骤向自己描述该过程,那么您将更接近您应该编写的代码,而不是与示例输出大致对应的代码行。
-
只有 4 个考试成绩,是的,这是我在尝试完成硬件问题时最失落的一次。
标签: python python-3.x