猿问

在这个Python家庭作业问题上遇到麻烦

问题:编写一个程序,以连续询问用户以整数百分比形式给出的考试分数,范围为 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)



BIG阳
浏览 101回答 4
4回答

繁花如伊

在这里,您的问题可以分为几个部分,从用户处获取考试次数从用户获取这些考试的有效输入计算所有考试的平均值计算等级分布如果用户输入为 -1,则退出下面的代码遵循所有这些步骤&nbsp;#calculating Average&nbsp; &nbsp; def calc_average(scores):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return sum(scores)/len(scores)&nbsp; &nbsp; grade_dist = {&nbsp; &nbsp; (90, 101):'A',&nbsp; &nbsp; (80,90):'B',&nbsp; &nbsp; (70, 80):'C',&nbsp; &nbsp; (59, 70):'D',&nbsp; &nbsp; (0,59):'F'&nbsp; &nbsp; }&nbsp; &nbsp; def get_grade_freq(scores):&nbsp; &nbsp; &nbsp; &nbsp; grades = {'A':0, 'B':0, 'C':0, 'D':0, 'F':0}&nbsp; &nbsp; &nbsp; &nbsp; for score in scores:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for k, v in grade_dist.items():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if score in range(k[0], k[1]):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grades[v]+=1&nbsp; &nbsp; &nbsp; &nbsp; print("Grade distributions")&nbsp; &nbsp; &nbsp; &nbsp; for grade, number in grades.items():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Number of {}’s = {}".format(grade, number))&nbsp; &nbsp; def get_scores(n):&nbsp; &nbsp; &nbsp; &nbsp; scores = []&nbsp; &nbsp; &nbsp; &nbsp; cond = True&nbsp; &nbsp; &nbsp; &nbsp; while cond and n>0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score = int(input("Enter an exam score between 0 and 100 or -1 to end : "))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if score==-1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cond=False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if score not in range(0,101):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Sorry, {} is not in the range of 0 and 100 or -1. Try Again!".format(score))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if score in range(0,101):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scores.append(score)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n-=1&nbsp; &nbsp; &nbsp; &nbsp; return scores&nbsp; &nbsp; def main():&nbsp; &nbsp; &nbsp; &nbsp; n = int(input('total number of exams ' ))&nbsp; &nbsp; &nbsp; &nbsp; scores = get_scores(n)&nbsp; &nbsp; &nbsp; &nbsp; if scores == -1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit(-1)&nbsp; &nbsp; &nbsp; &nbsp; average = calc_average(scores)&nbsp; &nbsp; &nbsp; &nbsp; print("You entered {} valid exam scores with an average of {}.".format(n, average))&nbsp; &nbsp; &nbsp; &nbsp; get_grade_freq(scores)&nbsp; &nbsp; if __name__=='__main__':&nbsp; &nbsp; &nbsp; &nbsp; main()

catspeake

每当有多个类似事物的实例需要操作(评分范围、总数)时,都必须尝试使用多值结构,而不是单个变量。Python的列表和字典旨在收集多个条目作为位置列表或键控索引(字典)。这将使您的代码更加通用。当您操作概念而不是实例时,您将知道自己走在正确的轨道上。例如:grading = [(None,101),("A",90),("B",80),("C",70),("D",60),("F",0)]scores&nbsp; = {"A":0, "B":0, "C":0, "D":0, "F":0}counts&nbsp; = {"A":0, "B":0, "C":0, "D":0, "F":0}while True:&nbsp; &nbsp; input_value = input("Enter an exam score between 0 and 100 or -1 to end: ")&nbsp; &nbsp; value&nbsp; &nbsp; &nbsp; &nbsp;= int(input_value)&nbsp; &nbsp; if value == -1: break&nbsp; &nbsp; score = next((s for s,g&nbsp; in grading if value >= g),None)&nbsp; &nbsp; if score is None:&nbsp; &nbsp; &nbsp; &nbsp; print("sorry ",input_value," is not -1 or in range of 0...100")&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; scores[score] += value&nbsp; &nbsp; counts[score] += 1inputCount = sum(counts.values())average&nbsp; &nbsp; = sum(scores.values())//max(1,inputCount)&nbsp;&nbsp;print("")print("You entered", inputCount, "valid exam scores with an average of: ", average)print("------------------------------------------------------------------------")print("Grade Distribution:")for grade,total in counts.items():&nbsp; &nbsp; print(f"Number of {grade}'s: ",total)该列表包含分数字母和最小值(以元组为单位)对。这样的结构将允许您通过查找值低于或等于输入值的第一个条目并使用相应的字母,将中的成绩值转换为评分字母。grading同一列表用于验证输入值,方法是有策略地将 None 值放在 100 之后,并且不低于零的值。该函数将为您执行搜索,并在不存在有效条目时返回 None。next()您的主程序循环需要继续,直到输入值为 -1,但它需要至少通过输入一次(典型的重复-直到结构,但在 Python 中只有一个)。因此,该语句将永远循环(即在 True 时),并且在满足退出条件时需要任意中断。whilewhile为了累积分数,字典 () 比列表更适合,因为字典将允许您使用键(分数字母)访问实例。这允许您跟踪单个变量中的多个分数。计算每个分数的输入数量也是如此。scores要获得最后的平均值,您只需将字典的值分数相加,然后除以添加到字典中的分数计数即可。scorescounts最后,要打印分数计数的摘要,您可以再次利用字典结构,并且只为所有分数字母和总计编写一个广义的打印行。

蓝山帝景

以下是我的演练解释:因此,该程序应该询问用户他们得到了什么分数,直到他们说他们得到了一个分数,在你给他们结果之后。要循环直到它们给出,我们可以使用一个循环:-1-1whileinp = float(input("Enter an exam score between 0 and 100 or -1 to end: ")) # Get inputgrades = []while inp > -1: # Loop until user gives a score of -1&nbsp; &nbsp; if inp >= 0 and inp <= 100: # Check if valid grade&nbsp; &nbsp; &nbsp; &nbsp; grades.append(inp) # Add grade to grades list&nbsp; &nbsp; &nbsp; &nbsp; inp = float(input("Enter an exam score between 0 and 100 or -1 to end: ")) # Ask again&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print("Sorry", inp, "is not in the range of 0 and 100 or -1. Try again!") # Invalid grade# ANALYSIS OF GRADES# Format first line of output - the first str(len(grades)) give the amount of grades they entered,# and the str(sum(grades) / len(grades)) gives the average of the grades list.print("You entered", str(len(grades)), "valid exam scores with an average of", str(sum(grades) / len(grades)))print("Grade Distribution:")print("Number of A's =", str(sum(90 <= g <= 100 for g in grades)) # I am using a very short notationprint("Number of B's =", str(sum(80 <= g <= 89 for g in grades)) # here - this is basically countingprint("Number of C's =", str(sum(70 <= g <= 79 for g in grades)) # the number of grades that areprint("Number of D's =", str(sum(60 <= g <= 69 for g in grades)) # a valid value based on the checksprint("Number of F's =", str(sum(0 <= g <= 59 for g in grades)) # I am making.希望我的评论能帮助你弄清楚我的代码中发生了什么!

天涯尽头无女友

这可能适用于您:scores = {&nbsp; &nbsp; "A": 0,&nbsp; &nbsp; "B": 0,&nbsp; &nbsp; "C": 0,&nbsp; &nbsp; "D": 0,&nbsp; &nbsp; "F": 0,}total = 0count = 0input_value = 0while (input_value != -1) and (count < 4):&nbsp; &nbsp; input_value = int(input("Enter an exam score between 0 and 100 or -1 to end: "))&nbsp; &nbsp; if 0 <= input_value <= 100:&nbsp; &nbsp; &nbsp; &nbsp; total += input_value&nbsp; &nbsp; &nbsp; &nbsp; count += 1&nbsp; &nbsp; &nbsp; &nbsp; if input_value >= 90:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scores["A"] += 1&nbsp; &nbsp; &nbsp; &nbsp; elif input_value >= 80:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scores["B"] += 1&nbsp; &nbsp; &nbsp; &nbsp; elif input_value >= 70:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scores["C"] += 1&nbsp; &nbsp; &nbsp; &nbsp; elif input_value >= 60:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scores["D"] += 1&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scores["F"] += 1&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; 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'])
随时随地看视频慕课网APP

相关分类

Python
我要回答