L2g2h
L2g2h

Reputation: 33

Python 3.3 Inner Nested While Loop Not Outputting

I'm trying to learn Python between self thought of projects relevant to me and utilizing teamtreehouse though it's slow progress.

Goal: Have inner loop calculate the cost of an individual class semester in one year then print that out. This inner loop will run 5 total times.

Outer loop should only run once just to print out the basic prints.

Instead I get this error though I defined the i (counter variable) as the first line of each while loop?

Error:

This program will display the projected semester tuition amount for the next 5 years for full-time students.                                                                    
This will be calculated with a $6,000 per semester tuition with a 2% yearly increase for 5 years.                                                                               
Traceback (most recent call last):                                                                                                                                              
  File "main.py", line 26, in <module>                                                                                                                                          
    while  i in range(1, years + 1):                                                                                                                                            
NameError: name 'i' is not defined

CODE

#////////MAIN PROGRAM START//////////////
print('This program will display the projected semester tuition amount for the next 5 years for full-time students.')
print('This will be calculated with a $6,000 per semester tuition with a 2% yearly increase for 5 years.')

#////////FORMULA START//////////////
#def formula():

#//////VARIABLES
#///increase of tuition %
yearlyIncrease=0.02
#///increase of tuition %

#/////counter variables
years=1
semester=1
semesters=2
#/////counter variables

tuitionTotalPre=0
tuitionCost=12000
tuitionTotalPost=0
semesterCost=0
#//////VARIABLES

#print(‘Tuition for ‘ year ‘is ‘ tuitionTotal
while  i in range(1, years + 1):
    i=0
    print('The cost of tuition for a semester this year is.')
    tuitionTotalPre=tuitionCost*yearlyIncrease
    tuitionTotalPost=tuitionCost+tuitionTotalPre
    tuitionCost=tuitionTotalPost
    semester=1
    while i in range(1, semesters + 1):
        i=0
        semesterCost=tuitionTotalPost/2 
        print(semesterCost)
        semester=semester+1
    years=years+1

#////////FORMULA END//////////////
#formula()

#////////MAIN PROGRAM END//////////////

Upvotes: 1

Views: 313

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122132

You wanted a for loop here:

for i in range(1, years + 1):

and

for i in range(1, semesters + 1):

for loops take an iterable (here the output of the range(1, years + 1) expression) and assigns each value produced by that iterable to the target variable (i).

A while loop takes a condition instead; an expression that controls wether or not the loop continues. If it is true, the loop body is run, otherwise it is not.

So in your case the while expression is i in range(1, years + 1), which asks if the value in the preexisting variable i is a member of the range(1, years + 1) outcome. Since you have no i variable defined before the while statement is entered, you get a NameError exception.

Next, you would not increment years and semester in the loop. Have range() produce all the numbers for you instead; if you have 3 years and 5 semesters, set those values first, so that you can generate a range to loop over:

years = 3
semesters = 5

for year in range(1, years + 1):
    # will loop through 1, 2, and 3
    for semester in range(1, semesters + 1):
        # will loop through 1, 2, 3, 4 and 5 for each year

Note that I picked more informative names here, i is not really a helpful name.

If you are familiar with the term, the Python for loop is a Foreach loop construct, and nothing like the C for construct.

Upvotes: 2

Related Questions