1

進捗は遅いですが、自分に関連するプロジェクトの自己考えとteamtreehouseの利用の間にPythonを学ぼうとしています。

目標: 内側のループで 1 年間の個々のクラスの学期の費用を計算し、それを出力します。この内側のループは合計 5 回実行されます。

外側のループは、基本的な印刷物を印刷するためだけに 1 回だけ実行する必要があります。

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

1 に答える 1