Я пытаюсь изучить Python между мыслями о проектах, имеющих отношение ко мне, и использованием teamtreehouse, хотя это медленный прогресс.
Цель: заставить внутренний цикл вычислить стоимость семестра отдельного занятия в течение одного года, а затем распечатать результат. Этот внутренний цикл будет выполняться всего 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//////////////