#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Advice on making a simple program


    im new to programming and my project is to make a gpa calculator. My teacher gave us a txt file to read/import over, so no inputs, just to convert the text to python i guess(?), with the student name, # of courses, credits, course name i just need to open them in python then calculate them as a gpa calculator. anyone can give me some basic info to give me a boost? i have a general idea of what to do but have a terrible time executing then in code. i dont want the answer, just some advice on what i could do to get started, thanks!

    heres the hw
    Objective:
    The purpose of this lab project is to expose you to selection, to enforce Loops, to use text file input/output, and to expand your skills to write larger programs.

    Problem Specification:
    Write a Python script to calculate a student’s GPA for the semester. The program should accept a student’s name (First then Last) and the number of courses he/she is taking, then for each course the following data is needed
    • the course number “A string”
    • course credits “an integer”
    • grade received for the course “a character string”.

    The program should write:
    • the student’s name (Last then “,” then first)
    • course title, number of credits for the course and the grade received all on one line (repeats for every course).
    • the total number of credits for the semester, grade point average for the semester and a warning message if the GPA is less than 2.0 or a congratulatory message if the GPA is 3.0 or above. (All are on one line)


    the grades.txt file to read is
    Student's Name Here
    5
    CIS 108
    3
    A
    MA 120
    4
    B
    Sc 104
    4
    C
    CIS 106
    1
    F
    SO 101
    3
    A
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Code:
    There is input to your program.  It's the file.
    
    When you read the file it will be string data.
    There need to be some rules about the input file
    so you can tell the course name from the number
    of credits from the grade.  And the student's
    name first and last.  After parsing the file use
    int(credits) to get a numerical value.
    [edit]Oh---I just saw the input file
    It's simple.[/edit]
    
    You may need to convert a letter grade to number.
    Have your lab partner do that.  Much too tricky.
    Or you could use a dictionary.  Similar to
    grades = {'A+':4.2, 'A':4, 'A-':3.8, 'B+':3.7}
    Then the conversion is grades[LETTER_GRADE].
    
    You'll need a formula to compute GPA.  It's a
    weighted average, thus the (dot product of
    numeric_grades with credits) divided by sum
    of the credits.
    
    You could use an if statement for the warning message.
    I wouldn't, though.  I'd write
    
    extra_message = ('warning','','congratulations')[(2 < GPA) + (3 < GPA)]
    Last edited by b49P23TIvg; May 8th, 2013 at 08:51 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0
    okay, thanks for the advice, much appreciated!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0
    heres what i have so far
    Code:
    #print title and open grades text file
    
    print ("Welcome to PCCC official GPA calculator")
    f = open ("grades.txt",'r',)
    
    
    print ("Student", f.readline()) #print Student and Students Name
    print ("No. Courses Taken:")  #print number of courses 
    
    f.close()
    SO FAR, i think im progressing slowly, but im getting the hang of it, the hard part will probably be the gpa calculator and output the gpa, any thoughts, or advice on what to do next? i think i have to convert the numbers from txt file to string?
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Superb.

    I'm quite sure you'll actually need to convert strings to numbers, because of the numerical computations that are most suitable for computing gpa.

    Keep trying your program and expressions with python on your computer.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0
    Code:
     
    #print title and open grades text file
    
    print ("Welcome to PCCC official GPA calculator")
    f = open ("grades.txt",'r',)
    
    
    print ("Student:", f.readline()) #print Student and Students Name
    print ("No. Courses Taken:", f.readline(2))  #print "number of courses" and the 2nd line for amount of courses
    
    
    def main():
        name = f.readline()
        numofcourses = int(f.readline())
        print (name, numofcourses)    
    
    
    
    
    f = open("grades.txt")
    
    for a in range(3):
        main()
    input()
          
    f.close()
    so im trying to grab the classes but i dont know if im even approaching this correctly. i feel so lost now xD, my professor doesnt do jack, theres so much material to learn.
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    I think you're not close, I think you don't test your program. Were I the professor a program like this would get high marks:
    Code:
    # python3 code
    
    def read_class(inf):
        name = inf.readline().strip()
        credits = int(inf.readline())
        letter_grade = inf.readline().strip()
        return (name,credits,letter_grade,)
    
    def convert(letter_grade):
        '''
            Without error handling, given a letter grade
            of A, B, C, D, or F return the common numeric
            equivalent.
            >>> convert('C')
            2
            >>> list(map(convert,'ABCDF'))
            [4, 3, 2, 1, 0]
        '''
        return 'FDCBA'.index(letter_grade)
    
    def main(file_name):
        cumulative_weighted_score = credits = 0
        with open(file_name) as f:
            name = f.readline().strip()
            numofcourses = int(f.readline())
            fields = name.split()
            first, last = fields[0], fields[-1]
            print('{}, {}'.format(last, first))
            for i in range(numofcourses):
                class_name, credit_hours, grade = read_class(f)
                #Lambert Electronics, USA, NY.
                numeric_grade = convert(grade)
                print('{:>7s}: {}, {}'.format(
                    class_name, credit_hours, grade))
                cumulative_weighted_score += credit_hours * numeric_grade
                credits += credit_hours
        gpa = cumulative_weighted_score / float(credits)
        message = (
            'Spend more time with professor office hours',
            '',
            'Challenge yourself next semester')[(2 < gpa) + (3 < gpa)]
        print()
        print('credits and gpa')
        print('{} {:.2f} {}'.format(credits, gpa, message))
    
    
    if __name__ == '__main__':
        print ("Welcome to PCCC official GPA calculator")
        main('grades.txt')
        input()
    With bad input this program crashes. Can you improve it to handle bad input? A more helpful response, one that blames the user error instead of program error would tell the line number in the input that isn't right.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    479
    Rep Power
    33
    You want to read from one student until the next student is found. How can you tell which line the student name is on? Is there some kind of delimiter, like an empty line between? I am also assuming that the "5" after "Student's Name Here" is a typo. I would use readlines() and iterate over the list but that is just personal preference.
    Last edited by dwblas; May 14th, 2013 at 01:10 PM.

IMN logo majestic logo threadwatch logo seochat tools logo