【问题标题】:Make a Program in Python that calculates the student's GPA?用 Python 编写一个计算学生 GPA 的程序?
【发布时间】:2019-07-12 02:14:20
【问题描述】:

我需要有关 Python 编码问题的帮助。

我必须计算学生的 GPA。程序必须询问他们上多少门课,然后要求他们输入每门课的成绩以及是否加权。

然后程序应该输出包括小数位在内的平均 GPA,我的主程序必须调用该函数。

该问题给出了与给定字母等级相关的非加权和加权数字分数的图表: gpa chart

这是我目前所拥有的:

def average (c): 
    div = c
    avg =(1.0*(sum(scores))/div)

#****************MAIN********************
c = input("How many classes are you taking?")
print ("You are taking " + str(c) + " classes.") 
x = input("Enter your letter grade.") 
w = int(input("Is it weighted? (1 = yes)")
    while (c <= 7): #Here it is saying that there is a syntax error because I input a while loop. I don't know why it is saying this. 
    if (x == A): 
        if (w == 1): 
            print ("Your GPA score is 5")
        else: 
            print ("Your GPA score is 4")
    elif (x == B): 
        if (w == 1): 
            print ("Your GPA score is 4")
        else: 
            print ("Your GPA score is 3") 

    elif (x == C): 
        if (w == 1): 
            print ("Your GPA score is 3")
        else: 
            print ("Your GPA score is 2") 

    elif (x == D): 
        if (w == 1): 
            print ("Your GPA score is 2") 
        else: 
            print ("Your GPA score is 1") 

    elif (x == F): 
        if ( w == 1): 
            print ("Your GPA score is 1")      
        else: 
            print ("Your GPA score is 0") 

    scores = []
    list.append(x)

    average(c)

任何帮助将不胜感激! :)

【问题讨论】:

  • 您具体要寻求什么帮助?
  • 我们这里没有水晶球。我们需要更多关于正在发生的事情的信息:(
  • 我在问我的代码是否可行。我正在通过 Edhesive 参加计算机科学入门课程,它的代码终端一直说存在“错误语法”,要求用户在主程序启动后立即输入 c 的值。我不知道该怎么办。任何帮助都会很棒!
  • 这不是你在 python 中调用函数的方式。 return 语句在定义时进入函数内部,然后在您的主块中使用 func() 或在您的情况下使用 classes(c)gpacalc(x,w) 调用函数
  • 非常感谢@G.Anderson!这很有帮助。

标签: python function return average gpa


【解决方案1】:

不确定您的要求,但在函数定义之后修复可能是一个好的开始

不要

def classes(c): 
print ("You are taking " + c  + " classes.") 

def classes(c): 
    print ("You are taking " + c  + " classes.") 

【讨论】:

  • 这个,印刷错误! python喜欢空格:D
  • 好的,这有帮助!谢谢你:)
【解决方案2】:

可能是这样的吗?我没有在类中划分任何内容,但我希望您了解其中的逻辑:

number_class=None

while number_class==None:# I set-it up number_class=None so it will keep asking the number of classes until you introduce an integer value
    try:
        number_class=input("How many classes are you taking?")
    except:
        print "Error, the input should be a number!\n"

total_result=0#your score start from 0
i=0
while i<number_class:# i create a loop to insert the score for each class
    grade=raw_input("Enter your letter grade for the %s class." %(str(i+1)))

    grade=grade.upper()#convert the grate to upper so you are able to introduce a or A without make too many check
    if grade== 'A' or grade== 'B' or grade== 'C' or grade== 'D' or grade== 'F':#if you introduce a correct score we add it to the total sum and we go ahead with next class
        i+=1#next iteration
        if grade== 'A':
            total_result+=4
        elif grade== 'B':
            total_result+=3
        elif grade== 'C':
            total_result+=2
        elif grade== 'D':
            total_result+=1
        #elif: grade== 'F':#we can omitt F seeing that it's =0
        #    total_result+=0
        print total_result
    else:# if you introduce a wrong input for the grade we ask you again without pass to next iteration
        print "Error, the grade should be: A, B, C, D or F!\n"


average="%.2f" % (float(total_result)/float(number_class))#we divided the total score for the number of classes using float to get decimal and converting on 2 decimal places
print "Your GPA is: %s" %(str(average))

例子:

How many classes are you taking?5
Enter your letter grade for the 1 class.A
4
Enter your letter grade for the 2 class.B
7
Enter your letter grade for the 3 class.C
9
Enter your letter grade for the 4 class.A
13
Enter your letter grade for the 5 class.B
16
Your GPA is: 3.20

【讨论】:

  • 非常感谢,这对您有很大帮助。但是,当我尝试在 CodeSkulptor 上运行它时,它要求每个班级的字母等级的输入不断重复而没有停止。有什么触发器可以阻止它吗?
  • 它询问您选择了多少个班级的成绩,如果您注意到它还看到了1班,二班等...我认为您最好删除成绩的总和,我打印了只是为了检查它是否正确
猜你喜欢
  • 2022-08-17
  • 2020-04-04
  • 2016-10-30
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多