在这个Python作业问题上有困难吗
问题:编写一个程序,连续向用户询问考试分数,分数以0到100之间的整数百分比表示。如果输入的值不在-1范围内,请打印出错误并提示用户重试。计算输入的所有有效等级的平均值以及每个字母等级类别中的等级总数,如下所示:90到100是A,80到89是B,70到79是C,60到69是D,0到59是F。使用负数作为前哨值以指示输入结束。负值仅用于结束循环,因此不要在计算中使用它。例如,如果输入为在这个Python作业问题上有困难吗,python,python-3.x,Python,Python 3.x,问题:编写一个程序,连续向用户询问考试分数,分数以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
#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)
注意:这是我第一次上计算机科学课,所以我肯定有一项明显的工作我没有完成,但我非常感谢能得到的任何帮助 因此,程序应该询问用户他们得到了多少分数,直到他们说他们得到了-1分,在你给他们结果之后。要循环直到它们给出-1,我们可以使用while循环: 希望我的评论能帮助您了解我的代码中发生了什么 这可能适合您:
scores = {
"A": 0,
"B": 0,
"C": 0,
"D": 0,
"F": 0,
}
total = 0
count = 0
input_value = 0
while (input_value != -1) and (count < 4):
input_value = int(input("Enter an exam score between 0 and 100 or -1 to end: "))
if 0 <= input_value <= 100:
total += input_value
count += 1
if input_value >= 90:
scores["A"] += 1
elif input_value >= 80:
scores["B"] += 1
elif input_value >= 70:
scores["C"] += 1
elif input_value >= 60:
scores["D"] += 1
else:
scores["F"] += 1
else:
print("Sorry", input_value, "is not in the range of 0 and 100 or -1. Try again!")
print("You entered {} valid exam scores with an average of: {}".format(count, total / count))
print("------------------------------------------------------------------------")
print("Grade Distribution:")
print("Number of A's: ", scores['A'])
print("Number of B's: ", scores['B'])
print("Number of C's: ", scores['C'])
print("Number of D's: ", scores['D'])
print("Number of F's: ", scores['F'])
这里你的问题可以分为几个部分 从用户处获取考试编号 从用户处获取这些考试的有效输入 计算所有考试的平均分 计算等级分布 如果用户输入为-1,则退出 下面的代码遵循所有这些步骤
#calculating Average
def calc_average(scores):
return sum(scores)/len(scores)
grade_dist = {
(90, 101):'A',
(80,90):'B',
(70, 80):'C',
(59, 70):'D',
(0,59):'F'
}
def get_grade_freq(scores):
grades = {'A':0, 'B':0, 'C':0, 'D':0, 'F':0}
for score in scores:
for k, v in grade_dist.items():
if score in range(k[0], k[1]):
grades[v]+=1
print("Grade distributions")
for grade, number in grades.items():
print("Number of {}’s = {}".format(grade, number))
def get_scores(n):
scores = []
cond = True
while cond and n>0:
score = int(input("Enter an exam score between 0 and 100 or -1 to end : "))
if score==-1:
cond=False
return -1
if score not in range(0,101):
print("Sorry, {} is not in the range of 0 and 100 or -1. Try Again!".format(score))
if score in range(0,101):
scores.append(score)
n-=1
return scores
def main():
n = int(input('total number of exams ' ))
scores = get_scores(n)
if scores == -1:
exit(-1)
average = calc_average(scores)
print("You entered {} valid exam scores with an average of {}.".format(n, average))
get_grade_freq(scores)
if __name__=='__main__':
main()
每当您有多个类似的实例来操纵评分范围、总计数时,您必须尝试使用多值结构,而不是单个变量。Python的列表和字典设计用于收集多个条目,作为位置列表或键控索引字典 这将使您的代码更加通用。当你操作概念而不是实例时,你就会知道你在正确的轨道上 例如:
grading = [(None,101),("A",90),("B",80),("C",70),("D",60),("F",0)]
scores = {"A":0, "B":0, "C":0, "D":0, "F":0}
counts = {"A":0, "B":0, "C":0, "D":0, "F":0}
while True:
input_value = input("Enter an exam score between 0 and 100 or -1 to end: ")
value = int(input_value)
if value == -1: break
score = next((s for s,g in grading if value >= g),None)
if score is None:
print("sorry ",input_value," is not -1 or in range of 0...100")
continue
scores[score] += value
counts[score] += 1
inputCount = sum(counts.values())
average = sum(scores.values())//max(1,inputCount)
print("")
print("You entered", inputCount, "valid exam scores with an average of: ", average)
print("------------------------------------------------------------------------")
print("Grade Distribution:")
for grade,total in counts.items():
print(f"Number of {grade}'s: ",total)
评分列表包含成对的分数字母和元组中的最小值。这样的结构将允许您通过查找值低于或等于输入值的第一个条目并使用相应的字母,将中的成绩值转换为分数字母
通过策略性地将“无”值置于100之后,将“无”值置于零之下,使用相同的列表验证输入值。下一个函数将为您执行搜索,并在没有有效条目时返回None
主程序循环需要继续,直到输入值为-1,但它至少需要遍历一次输入,这是repeat-until结构的典型特征,但在Python中只有一段时间。因此while语句将永远循环,即while True,并且在满足退出条件时需要任意中断
要累积分数,字典分数比列表更合适,因为字典允许您使用分数字母键访问实例。这允许您在单个变量中跟踪多个分数。计算每个分数的多少也是一样的
要获得最后的平均值,只需将分数字典中的值和分数相加,然后除以添加到计数字典中的分数计数
最后,要打印分数统计摘要,您可以再次利用字典结构,只为所有分数字母和总数写一行通用打印行。考试分数是否只有4分?或者多少考试分数?很抱歉,你的解决方案有很多问题。您似乎还没有完全理解这个问题或者如何为它编写Python解决方案——首先,您的解决方案假设应该始终有4个输入,但是如果只有3个输入呢?还是5?试着想想没有代码的任务,想象有人问你分数,把分数加起来,直到你告诉他们你完成了,然后计算结果。如果你自己用很小的步骤来描述这个过程,你将更接近你应该编写的代码,而不是对与示例输出大致对应的行进行编码。只有4个考试分数,是的,这是我在尝试完成硬件问题时丢失最多的分数。错误纠正在哪里,以确保值为-1或介于1-100之间???@AyushGarg为发现错误干杯!非常感谢您的回复。它一开始是有效的,但在我输入四个有效分数后,它会继续要求更多。不确定是什么原因造成的。@Mitchellsullivan该解决方案可以适用于任意数量的条目。如果您输入_值!=-1:在输入_值时发送到!=-1和计数<4:,以及一些小的调整,这可以限制为4个条目。我修改了答案,只支持4个为什么?这个代码应该是无限的,直到用户在输入中键入-1!
#calculating Average
def calc_average(scores):
return sum(scores)/len(scores)
grade_dist = {
(90, 101):'A',
(80,90):'B',
(70, 80):'C',
(59, 70):'D',
(0,59):'F'
}
def get_grade_freq(scores):
grades = {'A':0, 'B':0, 'C':0, 'D':0, 'F':0}
for score in scores:
for k, v in grade_dist.items():
if score in range(k[0], k[1]):
grades[v]+=1
print("Grade distributions")
for grade, number in grades.items():
print("Number of {}’s = {}".format(grade, number))
def get_scores(n):
scores = []
cond = True
while cond and n>0:
score = int(input("Enter an exam score between 0 and 100 or -1 to end : "))
if score==-1:
cond=False
return -1
if score not in range(0,101):
print("Sorry, {} is not in the range of 0 and 100 or -1. Try Again!".format(score))
if score in range(0,101):
scores.append(score)
n-=1
return scores
def main():
n = int(input('total number of exams ' ))
scores = get_scores(n)
if scores == -1:
exit(-1)
average = calc_average(scores)
print("You entered {} valid exam scores with an average of {}.".format(n, average))
get_grade_freq(scores)
if __name__=='__main__':
main()
grading = [(None,101),("A",90),("B",80),("C",70),("D",60),("F",0)]
scores = {"A":0, "B":0, "C":0, "D":0, "F":0}
counts = {"A":0, "B":0, "C":0, "D":0, "F":0}
while True:
input_value = input("Enter an exam score between 0 and 100 or -1 to end: ")
value = int(input_value)
if value == -1: break
score = next((s for s,g in grading if value >= g),None)
if score is None:
print("sorry ",input_value," is not -1 or in range of 0...100")
continue
scores[score] += value
counts[score] += 1
inputCount = sum(counts.values())
average = sum(scores.values())//max(1,inputCount)
print("")
print("You entered", inputCount, "valid exam scores with an average of: ", average)
print("------------------------------------------------------------------------")
print("Grade Distribution:")
for grade,total in counts.items():
print(f"Number of {grade}'s: ",total)