Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting error: Name error
#1
Hi^^

I am beginning to learn some coding with python and hence I am a total beginner.
I have not found in this community any beginner-section, so I really hope that this is the correct place to post
a noob question - I try my best to explain the issue as well as I can.


On Topic:


I am using Python-Version 3.7 (64-bit) on a Win10 OS.
Within the IDLE, I have opened a new file and typed it in this code (an exercise from a beginner book) :

if name == 'Alice':
    print('Hi Alice.')
elif age < 12:
        print('You are not Alice, kiddo.')
elif age > 2000:
            print('Unlike you, Alice is not an undead, immortal vampire.')
elif age > 100:
                print('You are not Alice, grannie.')
This was the output within the IDLE:

Output:
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> = RESTART: C:/Users/kr-ga/AppData/Local/Programs/Python/Python37/vampire.py = Traceback (most recent call last): File "C:/Users/kr-ga/AppData/Local/Programs/Python/Python37/vampire.py", line 1, in <module> if name == 'Alice': NameError: name 'name' is not defined >>>
The isolated error-message was that one:

Error:
Traceback (most recent call last): File "C:/Users/kr-ga/AppData/Local/Programs/Python/Python37/vampire.py", line 1, in <module> if name == 'Alice': NameError: name 'name' is not defined >>>
Im grateful for any help how to fix that.

regards,
Placebo
Reply
#2
Before you can use the name name in comparison you need to assign a value to it. There must be some code before that one, probably some code to take input from the user.

Recently there was another thread about this exercise from Automate the boring stuff. Maybe they want you to write the code before that one?

On page 39 of the book, first para, second line it says Pretend name was assigned some value earlier.
i.e. you are not expected to run the code, but understand how it will work, given name already has some value assigned in advance.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Your indention is wrong.

for example (added some lines to test it)

name = raw_input("What's your name:\n")
if name == 'Alice':
    print('Hi Alice.')
else:
    print("Hi, you're not Alice\nGoodbye ...")
    exit()
age = int(raw_input("What's your age:\n"))
if age < 12:
    print('You are not Alice, kiddo.')
elif age > 2000:
    print('Unlike you, Alice is not an undead, immortal vampire.')
elif age > 100 and age < 2001:
    print('You are not Alice, grannie.')
else:
    print('Your age is between 12 and 100')
Reply
#4
Hi!

First of all, thanks a lot to you both for replying and helping out:)

@buran:
I find it cool, that you right away have been able to identify where I am coming from and that I am a starter with the book from Al Sweigart. Seems that I am not the only one who has been starting out with this book.
I have it on Kindle, so it does not display page-values, but from the %-tage kindle shows me (reading progress), I would guess that I have missed this statement from the author („Pretend name was assigned some value earlier“).
Although, if I recall correctly, regards to his first practice (iirc ‚hello.py-file), he has written that in order to execute the code (into the idle), one needs to click F5 or ‚run code‘, which has suggested to me that the reader shall try to run it himself…I had obviously the same name-error regards tot he first practice, but have not known yet of a place, where starters can ask for help.
I will try to search for the thread, which you have mentioned.
Again, thanks a lot so far^^

@Axel_Erfurt:
Thanks a lot for your effort, much appreciated.
I have copied your code-suggestion and have ran it, but still get an error.
By the nature oft he error, I would guess, that ‚raw_input‘ is somehow an abstract value and not meant to be literally written down in the code?
Here is the error, when I copy/paste your code and run it:

Error:
Traceback (most recent call last): File "C:/Users/kr-ga/AppData/Local/Programs/Python/Python37-32/Vamp2.py", line 1, in <module> name = raw_input("What's your name:\n") NameError: name 'raw_input' is not defined >>>
I also post a screenshot from all - in case it might help to identify my mistake:

[Image: msdAF3j]
Reply
#5
It should be input, not raw_input, because you use python3 (that is the right thing to do).
raw_input is python2, in python3 it is just input

As a general remark - I would expect the author of the book will ask you to run the sample code in almost all of the cases. It just happens that in this particular case they try to explain a concept and are focused on this incomplete snippet. I will leave to you to decide if this is good or bad/confusing approach.
Here is one thread and one more
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Al right, thanks a lot for replying:)
Reply
#7
Hi!

Since, my concern is again about an exercise in the mentioned book here, I hope it is ok to not open a new thread, but instead using this one again.
Basically I start to question my sanity xD

I would like to just post a screenshot of a simple exercise and I would just like to see what you guys would make out of these exact instructions – if I am not mad, then in my opinion some info is missing and again also some values regards to the „input“-instructions?

This time by the way, I have managed to run the code without an error (due tot he help from yesterday), but basically I made some lines up and I would guess it is not really as it should be.
But to not give any bias, I would be really interested how experienced coders would proceed with the exercise, when they have only the instructions in the book available
–> here is the screenshot:

[Image: oonPDZR]
Reply
#8
The code is fine and it will run without any problems and without need to add anything else.

while True:
    print('Who are you?')
    name = input()
    if name != 'Joe':
        continue
    print('Hello, Joe. What is the password (It is a fish.)')
    password = input()
    if password == 'swordfish':
        break
print('Access granted.')
example output
Output:
Who are you? John Who are you? Joe Hello, Joe. What is the password (It is a fish.) Tuna Who are you? Joe Hello, Joe. What is the password (It is a fish.) swordfish Access granted.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
In fact, it all runs well.
For some unknown reason, I have always assumed that input() cannot be empty i.e. that some sort of vlaue must be within the brackets – please do not ask me why I could have assumed so xD
Nice, that clears a lot of previous struggle up – thanks a lot^^
Reply
#10
Actually, it's better to combine the print and input, not like they did. The code will be shorter...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pandas.errors.ParserError: Error tokenizing data. C error: Expected 9 fields in line Anldra12 9 15,089 Jun-15-2021, 08:16 AM
Last Post: Anldra12
  cx_Oracle.DatabaseError: Error while trying to retrieve text from error ORA-01804 rajeshparadker 0 8,595 Nov-12-2020, 07:34 PM
Last Post: rajeshparadker
  Coding error- Not sure where I have put error markers against the code that is wrong Username9 1 1,692 Sep-28-2020, 07:57 AM
Last Post: buran
  Type Error or Value Error? spalisetty06 3 2,343 Jul-21-2020, 04:56 AM
Last Post: deanhystad
  Error in Python3.6:free() Corrupted unsorted chunks error sameer_k 2 3,798 Mar-18-2020, 09:37 AM
Last Post: sameer_k
  ERROR:haproxystats:Error fetching stats deepakkr3110 1 1,986 Nov-12-2019, 12:29 PM
Last Post: Larz60+
  Error :unable to detect undefined names created in spyder ide error at line 2 milind_eac 2 8,204 Jul-30-2019, 10:29 PM
Last Post: milind_eac
  Getting error "Type error-a bytes-like object..." mrapple2020 1 5,340 Apr-06-2019, 06:37 PM
Last Post: mrapple2020
  Program gives error but doesn't say where the error is. FWendeburg 3 2,764 Mar-09-2019, 11:42 PM
Last Post: FWendeburg
  I have Traceback error and a type error and i dont know how to fix it coltron1282 2 2,471 Feb-07-2019, 03:15 PM
Last Post: coltron1282

Forum Jump:

User Panel Messages

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