Сделать программу на Python, которая рассчитывает средний балл студента? - PullRequest
0 голосов
/ 18 февраля 2019

Мне нужна помощь по вопросу кодирования в Python.

Я должен рассчитать средний балл студента.Программа должна спросить их, сколько классов они посещают, затем попросить их ввести оценки для каждого класса, и если он будет взвешенным.

Затем программа должна вывести усредненный средний балл, включая десятичное место, и мой основнойПрограмма должна вызвать функцию.

В этом вопросе приводится диаграмма для невзвешенных и взвешенных числовых баллов, которая коррелирует с данной буквенной оценкой: 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)

Любая помощь будет высоко ценится!:)

Ответы [ 2 ]

0 голосов
/ 18 февраля 2019

Нечто подобное может быть?Я ничего не делил на классы, но я хочу, чтобы вы поняли логику:

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
0 голосов
/ 18 февраля 2019

Не знаю, о чем вы просите, но исправление сразу после определения функций может быть хорошим началом.

Не

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

Do

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