Make A Program In Python That Calculates The Student's Gpa?
I am in need of assistance on a coding question in Python. I have to calculate a student’s GPA. The program must ask them how many classes they are taking, then ask them to ente
Solution 1:
Not sure about what you are asking for, but fixing just after the function definitions might be a good start
Don't
defclasses(c):
print ("You are taking " + c + " classes.")
Do
defclasses(c):
print ("You are taking " + c + " classes.")
Solution 2:
Something like that maybe? I didn't divide anything in classes but I want you to understand the logic:
number_class=Nonewhile number_class==None:# I set-it up number_class=None so it will keep asking the number of classes until you introduce an integer valuetry:
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=0while 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 checkif 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 iterationif grade== 'A':
total_result+=4elif grade== 'B':
total_result+=3elif grade== 'C':
total_result+=2elif grade== 'D':
total_result+=1#elif: grade== 'F':#we can omitt F seeing that it's =0# total_result+=0print total_result
else:# if you introduce a wrong input for the grade we ask you again without pass to next iterationprint"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 placesprint"Your GPA is: %s" %(str(average))
Example:
How many classes are you taking?5
Enter your letter grade for the 1class.A
4
Enter your letter grade for the 2class.B
7
Enter your letter grade for the 3class.C
9
Enter your letter grade for the 4class.A
13
Enter your letter grade for the 5class.B
16
Your GPA is: 3.20
Post a Comment for "Make A Program In Python That Calculates The Student's Gpa?"