This is a program that can calculate, the GPA of a student on a 5.0 scale,it's written in python 3.x and no modules are required.

2,402 Views
``````"""THIS A GPA CALCULATOR, IT TAKES THE SEMESTER
OF THE STUDENTS, THE NO OF COURSES, AND THE
AND PRINTS THE SEMESTER AND THE GPA

def conv(g):
if g=='A' :
return 5
elif g=='B':
return 4
elif g=='C':
return 3
elif g=='D':
return 2
elif g=='E':
return 1
elif g=='F':

#function that calculate the gpa based using while loop
def gpa(c_no):
k=0
tlu=0
t=0

while k!=c_no:
k=k+1
c=int(input('Course unit'))  #this line gets the unit for the course
tlu=c*conv(g)+tlu
t=t+c
gpa=round(tlu/t,2) #this line computes the gpa
print('You are in',sem,'Semester and your GPA is' ,gpa) #this line outputs the gpa and the semester
#function ends here

#this line tells the user the purpose of the program
print('This Program calculates the GPA of Students')
#this line tells the user the limitations of the program
print('The maximum amount of courses that this program can accept is 10 and the minimum is 2')
#this line prompts the user for the semester
sem=input('Which Semester')
#this line prompts the user for the number of courses offered that semester
c_no=int(input('Number of courses offered'))

if c_no==2: #if the no of courses inputed is 2
gpa(2)
elif c_no==3: #if the no of courses inputed is 3
gpa(3)
elif c_no==4: #if the no of courses inputed is 4
gpa(4)
elif c_no==5: #if the no of courses inputed is 5
gpa(5)
elif c_no==6: #if the no of courses inputed is 6
gpa(6)
elif c_no==7: #if the no of courses inputed is 7
gpa(7)
elif c_no==8: #if the no of courses inputed is 8
gpa(8)
elif c_no==9: #if the no of courses inputed is 9
gpa(9)
elif c_no==10: #if the no of courses inputed is 10
gpa(10)
else:
pass``````

Cool, calm, converted, and Computerized.

Guys please i need your contribution on one of my very first bug free programs in python.

This code can be simplified.

``````if 2 <= c_no <= 10:
gpa(c_no)
#
## replaces, (and note that the "pass" does nothing)
if c_no==2: #if the no of courses inputed is 2
gpa(2)
elif c_no==3: #if the no of courses inputed is 3
gpa(3)
elif c_no==4: #if the no of courses inputed is 4
gpa(4)
elif c_no==5: #if the no of courses inputed is 5
gpa(5)
elif c_no==6: #if the no of courses inputed is 6
gpa(6)
elif c_no==7: #if the no of courses inputed is 7
gpa(7)
elif c_no==8: #if the no of courses inputed is 8
gpa(8)
elif c_no==9: #if the no of courses inputed is 9
gpa(9)
elif c_no==10: #if the no of courses inputed is 10
gpa(10)
else:
pass``````

Also, you can use a dictionary to shorten the first block of code if you are familiar with dictionaries. And if you want input until a correct number is entered and also not error out when an alpha is entered:

``````c_no = 0
while c_no not in range(2, 11):
c_no=input('Number of courses offered')

try:
c_no = int(c_no)        ## allows for any alpha characters
except:
c_no = 0

gpa(c_no)``````
e-papa commented: Cool comment, hope to help others like you someday +1

Thanks so much, will try to modify and post again for crutiny, Thanks again I appreciate you construtive criticism.

This code can be simplified.

``````if 2 <= c_no <= 10:
gpa(c_no)
#
## replaces, (and note that the "pass" does nothing)
if c_no==2: #if the no of courses inputed is 2
gpa(2)
elif c_no==3: #if the no of courses inputed is 3
gpa(3)
elif c_no==4: #if the no of courses inputed is 4
gpa(4)
elif c_no==5: #if the no of courses inputed is 5
gpa(5)
elif c_no==6: #if the no of courses inputed is 6
gpa(6)
elif c_no==7: #if the no of courses inputed is 7
gpa(7)
elif c_no==8: #if the no of courses inputed is 8
gpa(8)
elif c_no==9: #if the no of courses inputed is 9
gpa(9)
elif c_no==10: #if the no of courses inputed is 10
gpa(10)
else:
pass``````

Also, you can use a dictionary to shorten the first block of code if you are familiar with dictionaries. And if you want input until a correct number is entered and also not error out when an alpha is entered:

``````c_no = 0
while c_no not in range(2, 11):
c_no=input('Number of courses offered')

try:
c_no = int(c_no)        ## allows for any alpha characters
except:
c_no = 0

gpa(c_no)``````

Thank you so much, but I've never seen inequalities used that way in python, will try it out, guess I limited python, It sure is very powerful and sly.

Thanks I tried the first comment you passed and improved on the, this is the code now

``````"""THIS A GPA CALCULATOR, IT TAKES THE SEMESTER
OF THE STUDENTS, THE NO OF COURSES, AND THE
AND PRINTS THE SEMESTER AND THE GPA

def conv(g):
if g=='A' :
return 5
elif g=='B':
return 4
elif g=='C':
return 3
elif g=='D':
return 2
elif g=='E':
return 1
elif g=='F':

#function that calculate the gpa based using while loop
def gpa(c_no):
k=0
tlu=0
t=0

while k!=c_no:
k=k+1
c=int(input('Course unit'))  #this line gets the unit for the course
tlu=c*conv(g)+tlu
t=t+c
gpa=round(tlu/t,2) #this line computes the gpa
print('You are in',sem,'Semester and your GPA is' ,gpa) #this line outputs the gpa and the semester
#function ends here

#this line tells the user the purpose of the program
print('This Program calculates the GPA of Students')
#this line tells the user the limitations of the program
print('The maximum amount of courses that this program can accept is 10 and the minimum is 2')
#this line prompts the user for the semester
sem=input('Which Semester')
#this line prompts the user for the number of courses offered that semester
c_no=int(input('Number of courses offered'))

if 2<= c_no<=10:
gpa(c_no)``````

Will try and read up on dictionaries, I like to understand what I do, I don't just write, I understand the basics first.
Thanks Again

One advantage of using a dictionary is that it is simple to add or delete options, so a similar method is used for menus. An example (not tested):

``````def conv(g):
grade_dict = {'A':5, 'B':4, 'C':3, 'D':2, 'E':1, 'F':0}
else:
print(g, "is not a valid letter grade")``````

Thanks been reading dictionaries today and have seen that it has some very specisl characteristic, of which the best is the fact that the in operation never slows down no matter how many items, because of the hashtable algorithm used, will definitely modify the code more and get back to you.

Well my GPA calculator is back and better please review and let me know better ways to optimize

``````"""THIS A GPA CALCULATOR, IT TAKES THE SEMESTER
OF THE STUDENTS, THE NO OF COURSES, AND THE
AND PRINTS THE SEMESTER AND THE GPA

def conv(g):
"""this function converts grade to their respective
grd={'A':5,'B':4,'C':3,'D':2,'E':1,'F':0}
if g in grd:
return grd[g]
else:

#function that calculate the gpa based using while loop
def gpa(c_no):
k=0
tlu=0
t=0

while k!=c_no:
k=k+1
c=int(input('Course unit'))  #this line gets the unit for the course
tlu=c*conv(g)+tlu
t=t+c
gpa=round(tlu/t,2) #this line computes the gpa
print('You are in',sem,'Semester and your GPA is' ,gpa) #this line outputs the gpa and the semester
#function ends here

#this line tells the user the purpose of the program
print('This Program calculates the GPA of Students')
#this line tells the user the limitations of the program
print('The maximum amount of courses that this program can accept is 10 and the minimum is 2')
#this line prompts the user for the semester
sem=input('Which Semester')
#this line prompts the user for the number of courses offered that semester
c_no=int(input('Number of courses offered'))

#this line computes the gpa depending on the no of courses
if 2<= c_no<=10:
gpa(c_no)``````