Search code examples
pythonloopsiterationaccumulator

I can't get my loop with accumulator to work properly, throwing error Python


I am trying to make a program to have a user input course name, number, grade, and credit hours to calculate the GPA and Quality points. I believe all calculations are correct, but I am not certain because my loop is broken. I am trying to make a loop where the iteration (ittNum) is accumulated and compared to the user's input of how many classes were take. Basically I want to have the loop and and for my data to be returned for use in the total calculation once the user has input the number of classes they took.

This is my latest error message.

Traceback (most recent call last): File "D:/Python/FinalProgram1.py", line 40, in ClassGPA() File "D:/Python/FinalProgram1.py", line 13, in ClassGPA ittNum = int('') ValueError: invalid literal for int() with base 10: ''

#Ask user's input for total classes taken
classesTaken = int(input('How many classes did you take? '))

#set function to take inputs and do calculations, should return values
#to variables defined
def ClassGPA():
    courseNum = input('What was the course number? ')
    courseName = input('What was the name of the course? ')
    courseGrade = input('What was your letter grade? ')
    courseHour = int(input('How many credit hours was the course worth? '))
    ittNum = int('')
    if courseGrade == ('A'): numGrade = 4.00
    elif courseGrade == ('A-'): numGrade = 3.67
    elif courseGrade == ('B+'): numGrade = 3.33
    elif courseGrade == ('B'): numGrade = 3.00
    elif courseGrade == ('B-'): numGrade = 2.67
    elif courseGrade == ('C+'): numGrade = 2.33
    elif courseGrade == ('C'): numGrade = 2.00
    elif courseGrade == ('C-'): numGrade = 1.67
    elif courseGrade == ('D+'): numGrade = 1.33
    elif courseGrade == ('D'): numGrade = 1.00
    elif courseGrade == ('F'): numGrade = 0.00
    qualityPoints = (numGrade * courseHour)
    print('Course:', courseNum, ', Course Name:', courseName, ', Quality Points:',\
          qualityPoints, ', GPA:', numGrade)
    ittNum = ittNum + 1
    totalHour += courseHour
    totalQual += qualityPoints
    totalGPA += numGrade
#Set loop condition
    if ittNum <= classesTaken:
        ClassGPA()
    else:
        return totalHour
        return totalQual
        return totalGPA

ClassGPA()

GPA = (totalGPA / totalQual)
print('Your total Quality Points for the semester were ', sum(totalQual))
print('Your total GPA for the semester was ', GPA```

Solution

  • Your error is here: ittNum = int('')

    You are trying to cast empty string to int, it cannot be done.

    EDIT:

    There are many more problems with this code.

    1. This isn't a loop. Instead, this is an improper use of recursion.
    2. Your variable classesTaken is never declared or initialized.
    3. The values this function returns (if it ever does, see problem #2) are never assigned to anything.
    4. You can only return once from a function. The two items returned after the first will never actually be returned.
    5. It appears you're trying to use the return variables totalGPA and totalQual, this will never work this way, see problem #4.