This code isn't mine, it belongs to someone else in my coding class. I'm relatively new and would like to know if there's any way to make the program any more efficient. It consists of several if, else, and elif statements and I would like to make it as concise as possible. The point is to create a simple GPA calculator in python with input statements.
print("Welcome to the GPA calculator!")
TotalClasses = int(input('How many total classes you have taken? '))
if TotalClasses>6:
print('You can not take more than 6 classes')
quit()
if TotalClasses > 0:
class1 = input("Grade for class #1: ")
if class1 == "A":
class1 = 4.00
elif class1 == "B":
class1 = 3.00
elif class1 == "C":
class1 = 2.00
elif class1 == "D":
class1 = 1.00
elif class1 == "F":
class1 = 0.00
else:
class1 = 0.00
if TotalClasses > 1:
class2 = input("Grade for class #2: ")
if class2 == "A":
class2 = 4.00
elif class2 == "B":
class2 = 3.00
elif class2 == "C":
class2 = 2.00
elif class2 == "D":
class2 = 1.00
elif class2 == "F":
class2 = 0.00
else:
class2 = 0.00
if TotalClasses > 2:
class3 = input("Grade for class #3: ")
if class3 == "A":
class3 = 4.00
elif class3 == "B":
class3 = 3.00
elif class3 == "C":
class3 = 2.00
elif class3 == "D":
class3 = 1.00
elif class3 == "F":
class3 = 0.00
else:
class3 = 0.00
if TotalClasses > 3:
class4 = input("Grade for class #4: ")
if class4 == "A":
class4 = 4.00
elif class4 == "B":
class4 = 3.00
elif class4 == "C":
class4 = 2.00
elif class4 == "D":
class4 = 1.00
elif class4 == "F":
class4 = 0.00
else:
class4 = 0.00
if TotalClasses > 4:
class5 = input("Grade for class #5: ")
if class5 == "A":
class5 = 4.00
elif class5 == "B":
class5 = 3.00
elif class5 == "C":
class5 = 2.00
elif class5 == "D":
class5 = 1.00
elif class5 == "F":
class5 = 0.00
else:
class5 = 0.00
if TotalClasses > 5:
class6 = input("Grade for class #6: ")
if class6 == "A":
class6 = 4.00
elif class6 == "B":
class6 = 3.00
elif class6 == "C":
class6 = 2.00
elif class6 == "D":
class6 = 1.00
elif class6 == "F":
class6 = 0.00
else:
class6 = 0.00
gpaTC1 = (class1) / TotalClasses
gpaTC2 = (class1 class2) / TotalClasses
gpaTC3 = (class1 class2 class3) / TotalClasses
gpaTC4 = (class1 class2 class3 class4) / TotalClasses
gpaTC5 = (class1 class2 class3 class4 class5) / TotalClasses
gpaTC6 = (class1 class2 class3 class4 class5 class6) / TotalClasses
if TotalClasses == 1:
print("Your GPA is " str(gpaTC1))
if TotalClasses == 2:
print("Your GPA is " str(gpaTC2))
if TotalClasses == 3:
print("Your GPA is " str(gpaTC3))
if TotalClasses == 4:
print("Your GPA is " str(gpaTC4))
if TotalClasses == 5:
print("Your GPA is " str(gpaTC5))
if TotalClasses == 6:
print("Your GPA is " str(gpaTC6))
CodePudding user response:
I ordinarily wouldn't do this, but what you posted is a perfect counter example of the programming principle DRY -- "don't repeat yourself". In your case, you were doing things 6 times, when in fact you could do them exactly once in a loop. This is the philosophy of your code, but written with all the repetitive stuff removed. Note that there's not even any need to limit things to 6 classes here. Also note the use of a dictionary to do the grade-to-GPA mapping. No need to have code for that.
The key thing is to identify what the processing is, and where it can be repeated, just repeat it generically. Also, ANY time you find yourself writing variable names like xxx1
, xxx2
, xxx3
etc. in Python, you need to replace that with a list or a loop. Every single time.
print("Welcome to the GPA calculator!")
grades = {
'A': 4.00,
'B': 3.00,
'C': 2.00,
'D': 1.00,
'F': 0.00
}
TotalClasses = int(input('How many total classes you have taken? '))
sumtotal = 0
for i in range(TotalClasses):
class1 = input(f"Grade for class #{i 1}: ")
sumtotal = grades[class1]
gpa = sumtotal / TotalClasses
print("Your GPA is",gpa)
CodePudding user response:
print("Welcome to the GPA calculator!")
grade = {'A':4.00, 'B':3.00, 'C':2.00, 'D':1.00, 'F': 0.00}
TotalClasses = int(input('How many total classes you have taken? '))
no = 0
for i in range(TotalClasses):
g = input(f"Enter Grade for class #{i 1}: ")
no = grade[g] if g in grade else 0.00
print(f"Your GPA is {no/TotalClasses}")
CodePudding user response:
print("Welcome to the GPA calculator!")
correctClassBool = False
grade_dictionary = {'A':4.0, 'B':3.0, 'C':2.0, 'D':1.0, 'F':0.0}
while(not correctClassBool):
TotalClasses = int(input('How many total classes you have taken? '))
if TotalClasses <= 0 or TotalClasses > 6:
print("Please input the right amount of classes")
else:
correctClassBool = True
grade_total = 0
for i in range(TotalClasses):
class = input(f"Grade for class #{i 1}: ")
try:
grade_total = grade_dictionary[class]
except Exception as e:
continue
gpa = grade_total / TotalClasses
print(f"Your GPA is {gpa}")