Python 3.3内部嵌套而循环不输出

时间:2015-04-26 01:27:10

标签: python while-loop python-3.3 loop-counter

我正在尝试在自我思考与我相关的项目和利用teamtreehouse之间学习Python,尽管进展缓慢。

目标:让内循环计算一年内个别课程学期的成本然后打印出来。这个内循环总共运行5次。

外循环只应运行一次,以打印出基本打印件。

相反,我得到了这个错误,虽然我将i(计数器变量)定义为每个while循环的第一行?

错误:

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//////////////

1 个答案:

答案 0 :(得分:2)

你想要一个for循环:

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

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

for循环采用可迭代(此处为range(1, years + 1)表达式的输出)并将该iterable生成的每个值分配给目标变量(i)。

while循环代替条件;一个控制循环的表达式继续循环。如果是,则循环体运行,否则不运行。

因此,在您的情况下,while表达式为i in range(1, years + 1),它会询问预先存在的变量i中的值是否为{{1}的成员结果。由于在输入range(1, years + 1)语句之前没有定义i变量,因此会出现while异常。

接下来,您不会在循环中增加NameErroryears。让semester为您生成所有数字;如果您有3年和5个学期,请设置第一个的值,以便您可以生成一个范围来循环:

range()

请注意,我在此处选择了更多信息丰富的名称,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 并不是真正有用的名称。

如果您熟悉该术语,则Python i循环为Foreach loop construct,而不像C for构造。

相关问题