我正在尝试在与我相关的项目的自我思考和利用 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
代码
#////////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//////////////