I have an assignment that is very similar to a past assignment. I have to create a for loop to find gpa.
The previous assignment was to use a while loop:

stop1 = int(input('How many GPAs do want to calculate? '))
totalgp = 0

totalgp1 = 0

while stop1 > 0 :
    stop = int(input('Enter the number of classes you have: '))
    while stop > 0:
        grade = input('Enter the grade recieved: ')
        if grade == 'A':
            grade = 4

        elif grade == 'B':
            grade = 3

        elif grade == 'C':
            grade = 2

        elif grade == 'D':
            grade = 1

        else:
            grade = 0
        units = int(input('Enter the amount of units for that class: '))

        GP = grade * units
        totalgp += GP
        totalgp1 += units
        stop -= 1



    GPA = round(totalgp/totalgp1, 2)
    print('Gpa: ', GPA)
    stop1 -= 1

I need to convert the two while loops into for loops, but i dont quite understand for loops. Help would be much appreciated

A for loop consists of 4 parts

for (Part1; Part2; Part3) {
    Part4
}

Part1 - the initializer. This is where you initialize variables, usually the loop counting variable but can be anything, really.
Part2 - the condition. This is where you put the condition that ends the loop. When it becomes false, the loop ends.
Part3 - the iterator. This is where you change the condition variables (usually by incrementing them) so that the loop will end, someday.
Part4 - the body. This is where the code you want executed goes.

Note that all these parts are optional, but we won't get into that here :)

Example:

for (int i = 0; i < 10; i++) {
    Console.WriteLine("{0} squared is {1}", i, i*i);
}

In this example we have:
Part 1: int i = 0
Part 2: i < 10
Part 3: i++
Part 4: the output statement

A while loop and a for loop are very similar, to the point that some compilers generate the same code for them.

The parts for a while loop:

Part 1
while (Part 2) {
    Part 4
    Part 3
}

And that's the same order they are executed in a for statement.

Edited 3 Years Ago by Ssyxz

Comments
It's an explantion, doesn't matter what language it's in
C not Python

i now have a new problem:

stop1 = int(input('How many GPAs do want to calculate? '))
totalgp = 0

totalgp1 = 0

for i in range(stop1)  :
    stop = int(input('Enter the number of classes you have: '))
    for e in range(stop) :
        grade = input('Enter the grade recieved: ')
        if grade == 'A':
            grade = 4

        elif grade == 'B':
            grade = 3

        elif grade == 'C':
            grade = 2

        elif grade == 'D':
            grade = 1

        else:
            grade = 0
        units = int(input('Enter the amount of units for that class: '))

        GP = grade * units
        totalgp += GP
        totalgp1 += units
        stop -= 1



    GPA = round(totalgp/totalgp1, 2)
    print('Gpa: ', GPA)
    stop1 -= 1 

it calculates gpa, but for the second, third, fourth,etc..., it combines all grades (e.g

"""How many GPAs do want to calculate? 2
Enter the number of classes you have: 2
Enter the grade recieved: A
Enter the amount of units for that class: 4
Enter the grade recieved: A
Enter the amount of units for that class: 4
Gpa:  4.0
Enter the number of classes you have: 2
Enter the grade recieved: C
Enter the amount of units for that class: 4
Enter the grade recieved: C
Enter the amount of units for that class: 4
Gpa:  3.0"""

)

Thanks in advance

You zero the totals before the loop instead of under it

for i in range(stop1)  :
    totalgp = 0
    totalgp1 = 0

    stop = int(input('Enter the number of classes you have: '))

and you can now delete

    stop -= 1

stop1 -= 1 

since the for() loop uses a different variable, i.e. a list of numbers.

Edited 3 Years Ago by woooee

This question has already been answered. Start a new discussion instead.