Wednesday, 2 October 2013

NameError when printing string's length based on input in python

NameError when printing string's length based on input in python

I'm a complete beginner in Python and I need some 'help' with something
which is relatively simple (for a non-beginner).
What I'm trying to make is a quick 'program' which measures the length of
a string which has been inputted. Maybe I have not looked hard enough, but
I can't seem to find any specific information about this on the interwebs.
Ok, so here is what I have done so far:
print "Please enter a number or word and I will tell you the length of it."
NR = raw_input()
print len(NR)
*NR has no significant meaning, it's just a random variable name
Everything works as expected at first. For example, I enter the word
"Hello" and it then replies with "5" or I enter the number 100 and it
replies with "3" which is great, but when I attempt to enter another word
I get this error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
hello
NameError: name 'hello' is not defined
However, when I enter another number (after I have already entered one),
it just repeats the number which I have entered. For example, when I first
enter the number "50" it replies with "2", but when I enter "50" a second
time it just repeats the integer to me.
Note: I think I understand the problem for the first part: It doesn't work
more than once because the variable "NR" only counts as the first string
which has been inputted. Even if I'm correct, I still don't know a
solution to this.

No comments:

Post a Comment