用Python编写一个计算学生的GPA的程序? [英] Make a Program in Python that calculates the student's GPA?
问题描述
我需要有关Python编码问题的帮助.
I am in need of assistance on a coding question in Python.
我必须计算学生的GPA.该程序必须询问他们要参加多少个班,然后要求他们输入每个班的成绩以及是否加权.
I have to calculate a student’s GPA. The program must ask them how many classes they are taking, then ask them to enter the grades for each class and if it is weighted.
然后程序应输出平均GPA(包括小数位),而我的主程序必须调用该函数.
The program should then output the averaged GPA including the decimal place, and my main program must call the function.
该问题给出了与给定字母等级相关的非加权和加权数字分数的图表: gpa图表
The question gives a chart for the non-weighted and weighted number scores that correlates with the given letter grade: gpa chart
这是我到目前为止的内容:
Here's what I have so far:
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)
任何帮助将不胜感激!:)
Any help would be much appreciated! :)
推荐答案
也许是这样?我没有在课程中划分任何内容,但我希望您理解其逻辑:
Something like that maybe? I didn't divide anything in classes but I want you to understand the logic:
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
这篇关于用Python编写一个计算学生的GPA的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!