Variable not defined (Python)

2024/7/8 6:40:20
FlightType=input("Which flight would you like to fly? Type '2 Seater', '4   Seater', or 'Historic'.")
# No validation included for the inputFlightLen=input("Would you like to book the '30' minutes flight or the '60'")
# No validation included for the inputif (FlightLen==30):MaxSlots=(600/FlightLen)elif (FlightLen==60):MaxSlots=(600//FlightLen)print (MaxSlots)

When I run the code, why does the following error message appear?

NameError: name 'MaxSlots' is not defined

Answer

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.

https://en.xdnf.cn/q/119530.html

Related Q&A

PyGame: draw.rect() has invalid parameters

Im trying to learn mouse events with PyGame, and Im trying to draw a box wherever the user clicks. Im setting a variable equal to pygame.mouse.get_pos(), and calling individual tuple members according …

Cant press enter in selenium2library

Im trying to make a test that will open Facebook, log in and search something. However Im having trouble getting Facebook to search. Selenium types whatever it needs in the search bar, but I cant find …

Converting string to datetime in Python using strptime

Im trying to convert the following String to datetime object in Python.datetime_object = datetime.strptime(Sat, 26 Nov 2016 15:17:00 +0000, %a, %b %d %Y %H:%c %z)I get the following error,File "&l…

Is there any differences between python2 and python3 about adding menu bar to Frame in tkinter?

Im trying to porting a project 2to3 on python, and stuck in tkinter.In python2, there is no problem with adding menu bar to Frame in tkinter,but python3 occured attribute error. (Frame object has no at…

Standard Input having weird characters with them in different programming lanuage

I am getting confused with the standard input of these programming languages : [Note:] I added details about so many programming languages as the problem with all of them is same in this matter and my …

How to turn a numpy array to a numpy object?

I have a NumPy array as follows: [[[ 0 0]][[ 0 479]][[639 479]][[639 0]]]and I would like to convert it into something like so: [( 0 0)( 0 479)(639 479)(639 0), dtype=dtype([(x, <i2), (y…

recover all line from an attribute in a database in json

To simplify my problem, I have a base in json, and I recover all of my lines of json to put informations in a base. It seems easy for moments, but problem is that my json is not correctly writtenSo i…

Calculate eigen value in python as same way(order) in Matlab

This is the Matlab code which is returning eigenvector in V and eigenvalue in D. Consider C is 9*9 matrix then V is 9*9 matrix and D is 9*9 diagonal. matrix.[V,D] = eig(C);I want the same thing in Pyth…

Python: ctypes and Pointer to Structure

I am trying to make a pointer of a struct and then de-reference it. But its crashing. I have mimiced the behvior here with this simple code. from ctypes import * import ctypesclass File(Structure):_fie…

Python:Why readline() function doesnt work for file looping

I have the following code:#!/usr/bin/pythonf = open(file,r)for line in f:print line print Next line , f.readline() f.close()This gives the following output:This is the first lineNext line That was the …