"""THIS A GPA CALCULATOR, IT TAKES THE SEMESTER
    OF THE STUDENTS, THE NO OF COURSES, AND THE
    COURSE UNITS AND RESPECTIVE GRADES,
    AND PRINTS THE SEMESTER AND THE GPA
        BY ADEGOKE OBASA,
    adegokeobasa@yahoo.com                """

#function to convert grade to grade point starts
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':
        return 0 #function to convert grade to grade point ends

#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
        g=input('Course grade')    #this line gets the grade 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

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)

Edited 5 Years Ago by woooee: n/a

Comments
Cool comment, hope to help others like you someday

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
    COURSE UNITS AND RESPECTIVE GRADES,
    AND PRINTS THE SEMESTER AND THE GPA
        BY ADEGOKE OBASA,
    adegokeobasa@yahoo.com                """

#function to convert grade to grade point starts
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':
        return 0 #function to convert grade to grade point ends

#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
        g=input('Course grade')    #this line gets the grade 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}
    if g in grade_dict:
        return grade_dict[g]
    else:
        print(g, "is not a valid letter grade")

Edited 5 Years Ago by woooee: n/a

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
    COURSE UNITS AND RESPECTIVE GRADES,
    AND PRINTS THE SEMESTER AND THE GPA
        BY ADEGOKE OBASA,
    adegokeobasa@yahoo.com                """

#function to convert grade to grade point starts
def conv(g):
    """this function converts grade to their respective
        grade points"""
    #this dictionary contains all the grade and grade points
    grd={'A':5,'B':4,'C':3,'D':2,'E':1,'F':0}
    if g in grd:
        return grd[g]
    else:
        print('invalid input', 'Grades are A,B,C,D,E,F')
#function to convert grade to grade point ends

#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
        g=input('Course grade')    #this line gets the grade 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)

Thanks in advance.

This code might look like the first one but using woooee suggestion, i read up on dictionaries and found out that it's on of the ways one could make a program faster, the in operation in dictionaries uses the hashtable algorithm so it nevers slows down, no matter how much the key-value is, so even though it looks the same lenght, it's actually faster, thanks again woooee.

To read up on hashtable algorithm click herehttp://en.wikipedia.org/wiki/Hash_table