Skip to content Skip to sidebar Skip to footer

Variable Not Defined (Python)

FlightType=input('Which flight would you like to fly? Type '2 Seater', '4 Seater', or 'Historic'.') # No validation included for the input FlightLen=input('Would you like to boo

Solution 1:

input() is always returned as a string and thus never equal to an integer.

The function then reads a line from input, converts it to a string (stripping a trailing newline)

See the documentation

Your if or elif is never true since an integer is not a string in the Python world (if you used an else it would always return that) so you never define the new variable (since it is never run). What you need to do is to convert each input() to an integer. This can be done using int() function:

FlightLen=int(input("Would you like to book the '30' minutes flight or the '60'"))

Here FlightLen has been converted to an integer once an input value has been given.


You do not need the () in the if elif statements if you are using Python 3 either:

if FlightLen==30:
elif FlightLen==60:

If you are using Python 2 print does not take an ()


You might also want to add an else to make sure FlightLen is always defined, ensuring you do not get this error.


Solution 2:

Use int function to convert it to integer and initialize MaxSlots variable with a value.

FlightType=int(input("Which flight would you like to fly? Type '2 Seater', '4   Seater', or 'Historic'."))

FlightLen=int(input("Would you like to book the '30' minutes flight or the '60'"))

MaxSlots = 0

if (FlightLen==30):
    MaxSlots=(600/FlightLen)

elif (FlightLen==60):
    MaxSlots=(600//FlightLen)

print (MaxSlots)

Post a Comment for "Variable Not Defined (Python)"