Joe Smart
Joe Smart

Reputation: 763

Variable defined in while loop cannot be defined

I have a while loop like so:

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

and later I want to use the values for h and phase but my IDE is telling me it cannot be defined. The values are being used in a calculation and the phase is printed.

Upvotes: 1

Views: 989

Answers (1)

Paco Abato
Paco Abato

Reputation: 4065

Define your variables outside the while block so they can be used outside such 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.'

Upvotes: 1

Related Questions