1

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//////////////
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
L2g2h
  • 35
  • 8
  • 1
    Please don't edit your question into a new question; with your comments I realised I missed a detail in your question, but lets keep this *re-usable for others* and not make the answers invalid by altering your code each time you hit a new problem. – Martijn Pieters Apr 26 '15 at 02:05

1 Answers1

2

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thank you very much Martijn! I've now converted this to be a for loop though now my confusion is if it is possible to just have the inner semester calculator loop run only once. Essentially one run of the inner nested for loop to every run of the outer for loop? – L2g2h Apr 26 '15 at 01:49
  • @L2g2h: but you have your ranges set up to only produce **one** integer. You don't increment the number in the loop, that's what the `range()` is supposed to do up front. – Martijn Pieters Apr 26 '15 at 02:03