Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError with simple code
#1
Hi, This is my first post on the forum.

I'm currently learning the basics of Python from a 2014 copy of "Idiot's Guides: Beginning Programming". I'm using Python 3.5.3 on Mac OS.

The book is using examples from Python 3.3.

One of the first examples in the book is a simple question and response piece of code (below).

input_var = input("What is your name? ")
print("Hello " + input_var)
I've saved that as a .py file and then gone to Terminal and changed the directory to the folder I have the file saved in.

I then open it up in Terminal and this happens...

python ch3_1.py
What is your name? Daniel
Traceback (most recent call last):
     File "ch3_1.py", line 1, in <module>
          input_var = input("What is your name? ")
     File "<string>", line 1, in <module>
NameError: name 'Daniel' is not defined
Why is this happening? I thought it should look something like this...

Output:
python ch3_1.py What is your name? Daniel Hello Daniel
Thanks in advance for any help or suggestions. Cheers, Dan :)

Just quick update. I was playing around with it and for some reason the code works when I type...

python ch3_1.py
What is your name? "Daniel"
Hello Daniel

Is that normal? I thought you didn't need to use quotation marks when responding only in the code?
Reply
#2
I can't tell why is this specific error message. But if you do python script_name.py, you run it true Python 2.x and there is no print function but statement.
victor@jerry:/tmp$ python untitled.py 
What is your name? Daniel
Traceback (most recent call last):
  File "untitled.py", line 1, in <module>
    input_var = input("What is your name? ")
  File "<string>", line 1, in <module>
NameError: name 'Daniel' is not defined
victor@jerry:/tmp$ python3 untitled.py 
What is your name? Daniel
Hello Daniel
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Please use code tags see: https://python-forum.io/misc.php?action=help&hid=25
Reply
#4
Thanks Larz60+. I'll use the code tags next time I post on here.

And thanks wavic. I uploaded the script using "python3" instead of just "python" and it seemed to work.
Reply
#5
In Python 2, input() considers that what is input is valid python code and tries to run it, so it thinks "Daniel" is a variable. In Python3, input() is just getting a string (in Python2 this would be raw_input()).
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple code JacobSkinner 1 315 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 480 Nov-07-2023, 04:32 PM
Last Post: snippsat
  help me simple code result min and max number abrahimusmaximus 2 902 Nov-12-2022, 07:52 AM
Last Post: buran
  Simple encoding code ebolisa 3 1,439 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,799 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple code question about lambda and tuples JasPyt 7 3,301 Oct-04-2021, 05:18 PM
Last Post: snippsat
  NameError issue with daughter's newb code MrGonk 2 1,449 Sep-16-2021, 01:29 PM
Last Post: BashBedlam
  My simple code don't works !! Nabi666 1 1,602 Sep-06-2021, 12:10 PM
Last Post: jefsummers
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,873 Jul-26-2021, 04:36 PM
Last Post: snippsat
Sad SyntaxError: from simple python example file from mind-monitor code (muse 2) warmcupoftea 4 2,823 Jul-16-2021, 02:51 PM
Last Post: warmcupoftea

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020