Home > Software design >  Why this NameError in IDLE shell?
Why this NameError in IDLE shell?

Time:10-21

print('What is your name?')    # ask for their name
myName = input()  
print('It is good to meet you, '   myName) 
print('The length of your name is:')
print(len(myName))

When I run it...

What is your name?
>>> bn
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    bn
NameError: name 'bn' is not defined

Version

>>> import sys; print(sys.version)
3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)]

CodePudding user response:

>>> bn

This means you typed bn on the interpreter shell, not to the previous input function....

You might want to write this instead of printing first

myName = input('What is your name? ')

Your error could also be Python2 related

Example

$ cat /tmp/app.py
print('What is your name?')    # ask for their name
myName = input()
print('It is good to meet you, '   myName)
print('The length of your name is:')
print(len(myName))
$ python2 /tmp/app.py
What is your name?
bn
Traceback (most recent call last):
  File "/tmp/app.py", line 2, in <module>
    myName = input()
  File "<string>", line 1, in <module>
NameError: name 'bn' is not defined
$ python3 /tmp/app.py
What is your name?
bn
It is good to meet you, bn
The length of your name is:
2

If you wanted to use Python2, you need to use raw_input() as input() implicitly calls eval() on the inputted values

CodePudding user response:

There must be a problem in your environment. You should consider uninstalling Python 2. But, also, try running this code and tell us what the output is:

import sys
print(sys.version[:5])
print('What is your name?')    # ask for their name
myName = input()  
print('It is good to meet you, '   myName) 
print('The length of your name is:')
print(len(myName))

Running this in IDLE from Py3.9, I get the following output:

Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929
64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or
"license()" for more information.
>>> 
==== RESTART: C:\Users\Gary\Google Drive\Projects\StackOverflow\69651615.py ====
3.9.7
What is your name?
bn
It is good to meet you, bn
The length of your name is: 2
>>>
  • Related