尝试创建一个简单的成绩簿程序

时间:2019-12-16 22:44:23

标签: python python-3.x

这是我到目前为止的代码。输入5个数字后,它会一直循环到开头。

创建成绩簿程序,该程序允许用户指定可能的最高成绩,然后输入各个作业的成绩。输入的分数不能超过指定的最高值或负分数。也想对成绩进行平均

def gradebook():
    while True:
        print("what is the highest grade possible")
        inp = int(input())
        if inp == "":
            cap = inp
            gradebook = []
        for x in range(5):
            l = int(input("enter grade"))
            if l > cap:
                print("you cannot have a grade higher than " + str(cap))
                gradebook.append(l)
                print(gradebook)

1 个答案:

答案 0 :(得分:0)

在这种情况下,while循环是正确的,在这种情况下,嵌套while循环。

代码

def gradebook(number_of_assignments):
    # init
    assignment_grade_list = []
    assignment_count = 1

    while True:
        try:
            # highest grade
            highest_grade = int(input("What is the highest grade possible?:"))
        except ValueError:
            print("Invalid input. The highest grade must be numeric.")
            continue
        else:
            while assignment_count in range(1,number_of_assignments+1):
                try:
                    # assignment grade
                    assignment_grade = int(input("Enter assignment #%s grade:" % str(assignment_count)))
                except ValueError:
                    print("Invalid input. The assignment grade must be numeric.")
                    continue
                if assignment_grade > highest_grade:
                    print("Invalid input. You cannot have a grade higher than %s." % str(highest_grade))
                    continue
                if assignment_grade < 0:
                    print("Invalid input. You cannot have a grade lower than zero.")
                    continue
                else:
                    assignment_grade_list.append(assignment_grade)
                    assignment_count+=1
            else:
                # average assignments
                return round(sum(assignment_grade_list)/len(assignment_grade_list),2)
                break

# gradebook
a = gradebook(5)
print('Average assignment score: %s' % a)