计算学生成绩的Python程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47764131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:21:51  来源:igfitidea点击:

Python program to calculate student grades

python

提问by user8422384

This is my first program in Python and I am having some trouble so forgive me if I have some simply syntax issues.

这是我在 Python 中的第一个程序,我遇到了一些问题,所以如果我有一些简单的语法问题,请原谅我。

I am writing a program that calculates a student's final score based on one exam grade worth 60% and 7 other test scores worth a combined 40% of the final grade. The user is asked to input the one exam score then asked to input the 7 test scores which are read in a loop. The letter grade is then determined from the final score calculated from the exam and tests. After that a grade comment is printed corresponding to the letter grade given to the student. This is my code so far:

我正在编写一个程序,根据一个占最终成绩 60% 的考试成绩和 7 个其他考试成绩计算学生的最终成绩,该成绩占最终成绩的 40%。要求用户输入一个考试分数,然后要求输入循环读取的 7 个考试分数。然后根据考试和测试计算的最终分数确定字母等级。之后,将打印与给予学生的字母等级相对应的等级注释。到目前为止,这是我的代码:

def read_test_scores() :

    print("ENTER STUDENT ID: ")
    id = int(input())

    print("ENTER EXAM SCORE: ")
    exam = int(input())

    print("ENTER ALL TEST SCORES: ")
    score1 = int(input())
    score2 = int(input())
    score3 = int(input())
    score4 = int(input())
    score5 = int(input())
    score6 = int(input())
    score7 = int(input())

    sum = (score1 + score2 + score3 + score4 + score5 + score6 + score7)

    tavge = sum/7
    return tavge

def compute_final_score(tavge, exam) :
    final_score = 0.4 * tavge + 0.6 * exam
    return final_score

def get_letter_grade(final_score) :

    if 90 <= final_score <= 100:
        grade = 'A'
    elif 80 <= final_score <= 89:
        grade = 'B'
    elif 70 <= final_score <= 79:
        grade = 'C'
    elif 60 <= final_score <= 69:
        grade = 'D'
    else:
        grade = 'F'
    return grade

def print_comment(grade) :

    if grade = 'A':
        print "COMMENT:            Very Good"
    elif grade = 'B':
        print "COMMENT:            Good"
    elif grade = 'C':
        print "COMMENT:            Satisfactory"
    elif grade = 'D':
        print "COMMENT:            Need Improvement"
    elif grade = 'F'
        print "COMMENT:            Poor"

read_test_scores()
print "TEST AVERAGE IS:    " + str(tavge)
compute_final_score()
print "FINAL SCORE IS:     " + str(final_score)
get_letter_grade(final_score)
print "LETTER GRADE IS:     " + str(grade)
print_comment(grade)

回答by am05mhz

there are many errors in your code, some of them are in the comment, but the most critical part is that you use global and local variables incorrectly

您的代码中有很多错误,其中一些在注释中,但最关键的部分是您错误地使用了全局和局部变量

here is and example of fixing your code using the correct way to use global variables.

这是使用正确使用全局变量的方法修复代码的示例。

https://repl.it/repls/SorrowfulOddballSongbird

https://repl.it/repls/SorrowfulOddballSongbird

tavge = 0
exam = 0
sid = 0
final_score = 0
grade = ''

def read_test_scores() :

    global sid
    print("ENTER STUDENT ID: ")
    sid = int(input())

    global exam
    print("ENTER EXAM SCORE: ")
    exam = int(input())

    print("ENTER ALL TEST SCORES: ")
    score1 = int(input())
    score2 = int(input())
    score3 = int(input())
    score4 = int(input())
    score5 = int(input())
    score6 = int(input())
    score7 = int(input())

    total = (score1 + score2 + score3 + score4 + score5 + score6 + score7)

    global tavge
    tavge = total/7
    #return tavge

def compute_final_score() :
    global final_score
    final_score = 0.4 * tavge + 0.6 * exam
    #return final_score

def get_letter_grade() :
    global grade
    if 90 <= final_score <= 100:
        grade = 'A'
    elif 80 <= final_score <= 89:
        grade = 'B'
    elif 70 <= final_score <= 79:
        grade = 'C'
    elif 60 <= final_score <= 69:
        grade = 'D'
    else:
        grade = 'F'
    #return grade

def print_comment() :
    if grade == 'A':
        print("COMMENT:            Very Good")
    elif grade == 'B':
        print ("COMMENT:            Good")
    elif grade == 'C':
        print ("COMMENT:            Satisfactory")
    elif grade == 'D':
        print ("COMMENT:            Need Improvement")
    elif grade == 'F':
        print ("COMMENT:            Poor")

read_test_scores()
print ("TEST AVERAGE IS:    " + str(tavge))
compute_final_score()
print ("FINAL SCORE IS:     " + str(final_score))
get_letter_grade()
print ("LETTER GRADE IS:     " + str(grade))
print_comment()

but you should consider using parameters instead using globals

但你应该考虑使用参数而不是使用全局变量

回答by B B

Here's my answer. The code should run. Notes are inserted as comments.

这是我的答案。代码应该运行。注释作为注释插入。

# NOTE: I haven't checked whether your math is right, or
# if the computed values are correct.  I did however get your
# script to work.


def read_test_scores():
    print("ENTER STUDENT ID: ")
    id = int(input())

    print("ENTER EXAM SCORE: ")
    exam = int(input())

    print("ENTER ALL TEST SCORES: ")
    score1 = int(input())
    score2 = int(input())
    score3 = int(input())
    score4 = int(input())
    score5 = int(input())
    score6 = int(input())
    score7 = int(input())

    sum = (score1 + score2 + score3 + score4 + score5 + score6 + score7)

    tavge = sum / 7.0

    # NOTE: if you want to use any variables from this function,
    # then you have to "bring them outside" by "returning"
    # them. Here, I return the values tavge, id, and exam. I noticed
    # that bringing out "exam" is necessary since you'll
    # be using it later on.
    return tavge, id, exam


def compute_final_score(tavge, exam):
    final_score = 0.4 * tavge + 0.6 * exam
    return final_score


def get_letter_grade(final_score):
    if 90 <= final_score <= 100:
        grade = 'A'
    elif 80 <= final_score <= 89:
        grade = 'B'
    elif 70 <= final_score <= 79:
        grade = 'C'
    elif 60 <= final_score <= 69:
        grade = 'D'
    else:
        grade = 'F'
    return grade


def print_comment(grade):
    # NOTE `=` is for assignment.  We use it when we want to
    # tell python to make a variable mean something. For example:
    # a = "some_name" basically means that when we call a, it would
    # return the string "some_name".

    # What you want to use here is `==` which is the equality operator.
    # This checks whether or thing are equal.
    if grade == 'A':
        print "COMMENT:            Very Good"
    elif grade == 'B':
        print "COMMENT:            Good"
    elif grade == 'C':
        print "COMMENT:            Satisfactory"
    elif grade == 'D':
        print "COMMENT:            Need Improvement"
    elif grade == 'F':
        print "COMMENT:            Poor"


# NOTE 1: you need to assign the function results to a
# variable (or variables), otherwise, the result or return value
# will go nowhere and you can't use it
tavge, id, exam = read_test_scores()
print "TEST AVERAGE IS:    " + str(tavge)

# NOTE 2: variable names do not have to be the same as
# the name in their respective functions.  Here, you can see
# that it will still run even if I changed the variable
# name final_score to my_variable.  Although, of course, using
# final_score would still work.

# NOTE 3: the final_score function requires 2 inputs,
# namely tavge and exam.  This basically means that you have to feed
# it with these 2 values for it to work.  I took the
# tavge and exam variables as the results from your read_test_scores
# function
my_variable = compute_final_score(tavge, exam)
print "FINAL SCORE IS:     " + str(my_variable)
grade = get_letter_grade(my_variable)
print "LETTER GRADE IS:     " + str(grade)
print_comment(grade)


# FINAL NOTE: I haven't commented regarding coding style etc (like say
# for instance, there are best practices regarding variable names
# within functions, that is, if they should be similar to variable names
# outside the function), but regardless, the code is a good start.  I
# would also advise you to try to narrow down your question first
# before posting.  This can be done by running your code, and searching
# the internet for the particular erro messages, and if you're still stuck,
# ask here on stackoverflow.

回答by chemical

As several people have mentioned you need to use == for comparison, you also are missing a colon after one of your if/else.

正如一些人提到的,您需要使用 == 进行比较,您的 if/else 之一之后也缺少一个冒号。

This is my take on your code. Keep in mind that this doesn't have and tests to make sure someone is actually entering in a number for a test score instead of text

这是我对你的代码的看法。请记住,这没有和测试以确保有人实际上输入了测试分数的数字而不是文本

"sum" is also the name of a built in function to Python, which sums up anything you provide it.

“sum”也是 Python 内置函数的名称,它总结了您提供的任何内容。

def read_test_scores():
    scores = []
    num_tests = 7
    print("ENTER ALL TEST SCORES: ")
    for i in range(num_tests):
        score = input("Test " + str(i + 1) + ":")
        scores.append(int(score))    

    return sum(scores) / num_tests    


def compute_final_score(average, exam_score):
    score = 0.4 * average + 0.6 * exam_score
    return score    


def get_letter_grade(finalized_score):
    if 90 <= finalized_score <= 100:
        letter_grade = 'A'
    elif 80 <= finalized_score <= 89:
        letter_grade = 'B'
    elif 70 <= finalized_score <= 79:
        letter_grade = 'C'
    elif 60 <= finalized_score <= 69:
        letter_grade = 'D'
    else:
        letter_grade = 'F'
    return letter_grade    


def print_comment(letter_grade):    
    if letter_grade == 'A':
        print("COMMENT:            Very Good")
    elif letter_grade == 'B':
        print("COMMENT:            Good")
    elif letter_grade == 'C':
        print("COMMENT:            Satisfactory")
    elif letter_grade == 'D':
        print("COMMENT:            Need Improvement")
    elif letter_grade == 'F':
        print("COMMENT:            Poor")    


def get_student_id():
    print("ENTER STUDENT ID: ")
    identity = int(input())
    return identity    


def get_exam_score():
    print("ENTER EXAM SCORE: ")
    exam_score = int(input())
    return exam_score    


if __name__ == '__main__':
    student_id = get_student_id()
    exam = get_exam_score()
    tavge = read_test_scores()
    print("TEST AVERAGE IS:    " + str(tavge))
    final_score = compute_final_score(tavge, exam)
    print("FINAL SCORE IS:     " + str(final_score))
    grade = get_letter_grade(final_score)
    print("LETTER GRADE IS:     " + str(grade))
    print_comment(grade)