无法定义 while 循环中定义的变量
Variable defined in while loop cannot be defined
我有一个像这样的 while 循环:
while True:
try:
h = int(raw_input("Please Enter your altitude in metres > "))
if h > 0 and h < 11000:
phase = 'Troposphere'
break
except ValueError:
print 'Your entered value contained letters or punctuation. Please enter a numerical value.'
之后我想使用 h
和 phase
的值,但我的 IDE 告诉我它无法定义。这些值正在计算中使用并打印阶段。
在 while 块之外定义您的变量,以便它们可以在此类块之外使用:
h = 0
phase = 0
while True:
try:
h = int(raw_input("Please Enter your altitude in metres > "))
if h > 0 and h < 11000:
phase = 'Troposphere'
break
except ValueError:
print 'Your entered value contained letters or punctuation. Please enter a numerical value.'
我有一个像这样的 while 循环:
while True:
try:
h = int(raw_input("Please Enter your altitude in metres > "))
if h > 0 and h < 11000:
phase = 'Troposphere'
break
except ValueError:
print 'Your entered value contained letters or punctuation. Please enter a numerical value.'
之后我想使用 h
和 phase
的值,但我的 IDE 告诉我它无法定义。这些值正在计算中使用并打印阶段。
在 while 块之外定义您的变量,以便它们可以在此类块之外使用:
h = 0
phase = 0
while True:
try:
h = int(raw_input("Please Enter your altitude in metres > "))
if h > 0 and h < 11000:
phase = 'Troposphere'
break
except ValueError:
print 'Your entered value contained letters or punctuation. Please enter a numerical value.'