Input() Error - Nameerror: Name '...' Is Not Defined

Python get input from user - error name not defined

Use raw_input() instead, since you're using Python 2.7.

raw_input gets the input as text (i.e. the characters that are typed), but it makes no attempt to translate them to anything else; i.e. it always returns a string.

input gets the input value as text, but then attempts to automatically convert the value into a sensible data type; so if the user types ‘1’ then Python 2 input will return the integer 1, and if the user types ‘2.3’ then Python 2 input will return a floating point number approximately equal to 2.3

input is generally considered unsafe; it is always far better for the developer to make decisions about how the data is interpreted/converted, rather than have some magic happen which the developer has zero control over.

It is the reason why that automatic conversion has been dropped in Python 3 - essentially; - raw_input in Python 2 has been renamed to input in Python 3; and there is no equivalent to the Python 2 input magic type conversion functionality.

int(input()) error - NameError: name '…' is not defined

This is because your variables are in a local scope. You can't access x outside of the ask_x() function.

I would suggest you read up on functions to get a better grasp of this.

def ask_x():
return int(input('What is X?'))

def ask_y():
return int(input('What is Y?'))

def result(z):
print(z)

def count(x, y):
if (x>10):
return x + y
else:
print('nono')
return 0


#start of program
x = ask_x()
y = ask_y()
z = count(x, y)
result(z)

This will grab the values in each function, however, instead of storing them in the local scope, it'll be returned to the main function and stored in the corresponding variable.

You can then send x and y as parameters to count(), take care of your logic, and return the the value to be stored as z.

I hope this makes sense!

NameError: name 'InputNumber' is not defined

The main issue is you're attempting to use a function before it's been defined. Move your definition of InputNumber to the top of your file; or at least before it's used.

The other fix that you should begin practicing is to wrap all code in functions. All the code that you have at the top level that aren't function definitions should ideally be put into a main function or something similar. That will solve your problem here, and also ease development later if you start developing using a REPL. By having all that code outside of functions, you're forcing it to run when the file is read, which is sub-optimal in any code that's not a simple test or toy.


Other notes:

  • You define InputNumber twice in two different spots, and the first definition only happens if StartQuestion == 'a'. Don't do this. Conditionally defining variables/functions will make your code prone to NameErrors, since you allow for the possibility of using a function that wasn't defined. Just define it the once at the top.

  • Variable names should be in lower_snake_case. UpperCamelCase is reserved for class names.

  • Once this code is working and complete, you can post it over on Code Review, and people can make further suggestions.

Name error: 'self' not defined - when calling a function to create in-class variables

The way you have it, vocabulary, vocabulary_size = self.get_vocabulary() is being executed when the class is being defined, so there is no self. The latter is the name of the first argument passed to methods of a class and is the instance of the class (that was previously created) on which to operate.

The proper way to do this would be to call get_vocabulary() method from the __init__() method when an instance of the class exists and is being initialized.

Here's what I mean:

class Documents:
def __init__(self, input_file):
self.input_file_ = input_file # List in which each element is a list of tokens
self.vocabulary, self.vocabulary_size = self.get_vocabulary()

assert type(self.input_file_) is list, 'Input file is not a list'
assert type(self.input_file_[0]) is list, 'Elements in input file are not lists' # Only checks first instance, not all. But should suffice

def get_vocabulary(self):
vocabulary = set([el for lis in self.input_file_ for el in lis])
return vocabulary, len(vocabulary)

Comment (off-topic):

In languages that have classes and support object-orientated code like Python, it's usually best to avoid type-checking as much as possible because it doesn't support subtyping — but you can overcome that limitation when it is done by using the built-in isinstance() built-in function.

This implies that it would probably be better to do the following in your __init__() method:

    assert isinstance(self.input_file, list), 'Input file is not a list'
assert isinstance(self.input_file_[0], list), 'Elements in input file are not lists' # Only checks first instance, not all. But should suffice


Related Topics



Leave a reply



Submit