Python Forum
Getting error: Name error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Getting error: Name error (/thread-12685.html)

Pages: 1 2


Getting error: Name error - Placebo - Sep-07-2018

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


RE: Getting error: Name error - buran - Sep-07-2018

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.


RE: Getting error: Name error - Axel_Erfurt - Sep-07-2018

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')



RE: Getting error: Name error - Placebo - Sep-07-2018

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]


RE: Getting error: Name error - buran - Sep-07-2018

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


RE: Getting error: Name error - Placebo - Sep-07-2018

Al right, thanks a lot for replying:)


RE: Getting error: Name error - Placebo - Sep-08-2018

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]


RE: Getting error: Name error - buran - Sep-08-2018

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.



RE: Getting error: Name error - Placebo - Sep-08-2018

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^^


RE: Getting error: Name error - buran - Sep-08-2018

Actually, it's better to combine the print and input, not like they did. The code will be shorter...