I\'m having trouble writing a GPA program in Python. Seeing as I can\'t use def
ID: 3677207 • Letter: I
Question
I'm having trouble writing a GPA program in Python. Seeing as I can't use def or class, I'm stuck. If you can help, I'd appreciate it.
print("Welcome to the GPA Program.")
prompt = "What's the name of the transcript file? "
total_hours = 0
file_name = input(prompt)
in_file = open(file_name,"r")
for course in in_file:
dept, course_num, hours, grade = course.split(' ')
output = "{} {} {} {}".format(dept, course_num, hours, grade)
print(output,end = '')
total_hours = total_hours + int(hours)
total_points = 0
if grade == "A":
points = total_points + 4 * int(hours)
elif grade == "B":
points = total_points + 3 * int(hours)
elif grade == "C":
points = total_points + 2 * int(hours)
elif grade == "D":
points = total_points + 1 * int(hours)
elif grade == "F":
points = 0
total_points = points/4
print("Your GPA is", total_points/total_hours)
pass
in_file.close()
print("Your total hours are", total_hours)
print("Thanks for using the GPA Program!")
Explanation / Answer
grade_mapping = {'A ':4, 'B ':3, 'C ':2, 'D ':1, 'F ':0,'A':4, 'B':3, 'C':2, 'D':1, 'F':0}
# while reading from the file the grades are getting stored in the form of A , B until the end of file
# we can match both A and A with 4 and similarly for other grades as well
total_points = 0.0
total_hours = 0
print "Welcome to the GPA Program."
prompt = "What's the name of the transcript file? "
file_name = raw_input(prompt)
in_file = open(file_name,"r")
for line in in_file: # reading from the file line by line
elems = line.split(' ') # splitting with space
# finding the grade value corresponding to the grade and multiplying it with hours
total_points = total_points + grade_mapping[elems[3]] * int(elems[2])
total_hours = total_hours + int(elems[2]) # finding total hours
in_file.close()
print " Your GPA is", total_points/total_hours
print "Your total hours are", total_hours
print "Thanks for using the GPA Program!"
Assuming the input file is of the format
CS 1 2 A
EE 2 1 B
HS 3 2 C
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.