首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Python中制作成绩簿?

在Python中制作成绩簿?
EN

Stack Overflow用户
提问于 2018-06-13 07:06:33
回答 2查看 0关注 0票数 0

我是python的新手,正在努力打造一本成绩簿。这些是说明。

  1. 输入学生的姓名。退出是否输入名称“-1”。
  2. 为每个学生输入分数。
  3. 计算输入等级数的平均值。输出学生的姓名和信件成绩。标准等级分配保持不变(即在90以上是'A',低于90但在80以上是'B'等)。
  4. 输入下一个学生的姓名并重复输入退出条件。

例:

代码语言:javascript
复制
Enter the student's name:Bobby Baylor

How many grades does Bobby Baylor have?4

Enter grade 0:100

Enter grade 1:80

Enter grade 2:90

Enter grade 3:90
Bobby Baylor made A

Enter the student's name:Betty Baylor

How many grades does Betty Baylor have?3

Enter grade 0:100

Enter grade 1:80

Enter grade 2:80
Betty Baylor made B

Enter the student's name:-1

这是我尝试过的代码,但是我不断收到一个错误,那就是我的第一条if语句无效。我相信有更多的错误,但我无法超越这第一个错误。

代码语言:javascript
复制
def main():


    while true:
        student_name=(input("Enter the student's name:")
        if student_name==-1:
           break
        else:
            num_exams=int(input("How many grades does"+student_name+"have?"))
            counter=0
            test_num=int(input('Enter grade'+counter+':'))
                while counter<num_exams-1:
                    if counter < num_exams:
                       counter=counter+1
                       test_num=int(input('Enter grade'+counter+':'))
                for n in range(test_num):
                    total_exams+=test_num
                    avg=total_exams/num_exams   

                if avg>=90:
                    print(student_name, "made A")
                elif 80<=avg<90:
                    print(student_name, "made B")
                elif 70<=avg<80:
                    print(student_name, "made C")
                elif 60<=avg<70:
                    print(student_name, "made D")

main()
EN

回答 2

Stack Overflow用户

发布于 2018-06-13 15:22:23

代码语言:txt
复制
while True:
    student_name=(input("Enter the student's name:"))
    if student_name=='-1':
        break
    else:
        num_exams=int(input("How many grades does"+student_name+"have?"))
        counter=0
        test_num=[]
        total_exams=0
        while counter < num_exams:
            counter =counter+1
            test_num.append(int(input('Enter grade'+ str(counter)+':')))
        for n in range(0,num_exams):
            total_exams+=test_num[n]

        avg=total_exams/num_exams  
        if avg>=90:
            print(student_name, "made A")
        elif 80<=avg<90:
            print(student_name, "made B")
        elif 70<=avg<80:
            print(student_name, "made C")
        elif 60<=avg<70:
            print(student_name, "made D")
        else:
            print("Fail")
票数 0
EN

Stack Overflow用户

发布于 2018-06-13 16:44:16

代码语言:txt
复制
def main():
    while True:   # correct spelling
        student_name= input("Enter the student's name:")  # fixed ( not needed
        if student_name == "-1":   # this is a string, not a number
            break # you could as well return None here

        # else:  # as pointed out by Rishav this is not needed

        num_exams=int(input("How many grades does "+student_name+ " have?"))
        # list comp that uses range() to ask c times
        # fix for the string concattenation (cant do "x" + 3 + "x")
        grades = [int(input('Enter grade '+str(c+1)+':')) for c in range(num_exams)]
        total = sum(grades) # use build-in to summ the grades in list
        average = total / num_exams   # calc average
        if average >=90:  # no need to double check, first one will be usd
            print(student_name, "made A")
        elif average>=80:  # so comparing for <90 is not needed at all
            print(student_name, "made B")
        elif average >=70 :
            print(student_name, "made C")
        else:


  print(student_name, "made D")
main()

你可以用:

代码语言:txt
复制
    # grades = [int(input('Enter grade '+str(c+1)+':')) for c in range(num_exams)]

    grades = []

    for k in range(num_exams): # range does 0... num_exams-1
        grades.append(int(input("Enter grade {}:".format(k+1))))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档