【发布时间】:2015-04-26 01:27:09
【问题描述】:
我正在尝试在自我思考与我相关的项目和利用 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//////////////
【问题讨论】:
-
请不要将您的问题编辑为新问题;与您的 cmets 一起,我意识到我错过了您问题中的一个细节,但让我们保持这个对其他人可重复使用,并且不要在每次遇到新问题时通过更改代码来使答案无效。
标签: python while-loop python-3.3 loop-counter