用于计算学生成绩的Python程序

时间:2017-12-12 01:42:04

标签: python

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

我正在编写一个程序,根据一个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)

3 个答案:

答案 0 :(得分:0)

您的代码中存在许多错误,其中一些错误在评论中,但最关键的部分是您错误地使用了全局变量和局部变量

这是使用正确的方法来修复代码的示例。

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()

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

答案 1 :(得分:0)

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

# 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.

答案 2 :(得分:0)

正如有几个人提到你需要使用==进行比较,你也会在你的if / else之后错过一个冒号。

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

“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)