【问题标题】:Python 3.3 Inner Nested While Loop Not OutputtingPython 3.3内部嵌套while循环不输出
【发布时间】: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


【解决方案1】:

你想要一个 for 循环在这里:

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

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

for 循环采用一个可迭代对象(这里是 range(1, years + 1) 表达式的输出)并将该可迭代对象生成的每个值分配给目标变量 (i)。

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

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

接下来,您不会在循环中增加 yearssemester。让range() 为您生成所有数字;如果您有 3 年 5 个学期,请设置这些值首先,以便您可以生成一个循环范围:

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

请注意,我在这里选择了更多信息性名称,i 并不是一个真正有用的名称。

如果您熟悉这个术语,那么 Python for 循环是一个 Foreach loop construct,与 C 中的 for 构造完全不同。

【讨论】:

  • 非常感谢Martijn!我现在已经将它转换为一个 for 循环,但现在我的困惑是是否可以只让内部学期计算器循环运行一次。基本上每次运行外部 for 循环都会运行一次内部嵌套 for 循环?
  • @L2g2h:但您将范围设置为仅生成 一个 整数。您不会在循环中增加数字,这就是 range() 应该预先做的事情。
猜你喜欢
  • 2019-07-12
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
相关资源
最近更新 更多