Python 3.3 Inner Nested While Loop Not Outputting -


i'm trying learn python between self thought of projects relevant me , utilizing teamtreehouse though it's slow progress.

goal: have inner loop calculate cost of individual class semester in 1 year print out. inner loop run 5 total times.

outer loop should run once print out basic prints.

instead error though defined i (counter variable) first line of each while loop?

error:

this program display projected semester tuition amount next 5 years full-time students.                                                                     calculated $6,000 per semester tuition 2% yearly increase 5 years.                                                                                traceback (most recent call last):                                                                                                                                                 file "main.py", line 26, in <module>                                                                                                                                               while  in range(1, years + 1):                                                                                                                                             nameerror: name 'i' not defined 

code

#////////main program start////////////// print('this program display projected semester tuition amount next 5 years full-time students.') print('this calculated $6,000 per semester tuition 2% yearly increase 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 ‘ year ‘is ‘ tuitiontotal while  in range(1, years + 1):     i=0     print('the cost of tuition semester year is.')     tuitiontotalpre=tuitioncost*yearlyincrease     tuitiontotalpost=tuitioncost+tuitiontotalpre     tuitioncost=tuitiontotalpost     semester=1     while in range(1, semesters + 1):         i=0         semestercost=tuitiontotalpost/2          print(semestercost)         semester=semester+1     years=years+1  #////////formula end////////////// #formula()  #////////main program end////////////// 

you wanted for loop here:

for in range(1, years + 1): 

and

for in range(1, semesters + 1): 

for loops take iterable (here output of range(1, years + 1) expression) , assigns each value produced iterable target variable (i).

a while loop takes condition instead; expression controls wether or not loop continues. if true, loop body run, otherwise not.

so in case while expression i in range(1, years + 1), asks if value in preexisting variable i member of range(1, years + 1) outcome. since have no i variable defined before while statement entered, nameerror exception.

next, not increment years , semester in loop. have range() produce numbers instead; if have 3 years , 5 semesters, set values first, can generate range loop over:

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

note picked more informative names here, i not helpful name.

if familiar term, python for loop foreach loop construct, , nothing c for construct.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -