gpt4 book ai didi

python - while 循环中定义的变量无法定义

转载 作者:太空宇宙 更新时间:2023-11-03 17:53:42 26 4
gpt4 key购买 nike

我有一个像这样的 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.'

后来我想使用hphase的值,但我的IDE告诉我它无法定义。这些值正在用于计算并打印相位。

最佳答案

在 while block 之外定义变量,以便可以在该 block 之外使用它们:

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.'

关于python - while 循环中定义的变量无法定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28787682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com