Python Forum
"NameError: name 'catName1' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"NameError: name 'catName1' is not defined
#1
Greetings everybody,
First off, I'm a 71 year old retired male at home trying to learn python. No schools involved. I just thought this was the best forum to start. I'm using a book to study with. I’m running
MX Linux on a dell laptop. Python 3.5
I did this:
def exponents( num1, num2, num3 ):   
   
    answer1 = num1 * num1
    answer2 = num2 ** 3
    answer3 = num3 ** 4

    return answer1, answer2, answer3

userNumber1 = input( "Enter a number: \n" )
userNumber2 = input( "Enter a number: \n" )
userNumber3 = input( "Enter a number: \n" )

userNumber1 = float( userNumber1 )
userNumber2 = float( userNumber2 )
userNumber3 = float( userNumber3 )

numberExponentiated = exponents( userNumber1, userNumber2, userNumber3 )


print( " The result of your Exponential Machinations is:", numberExponentiated )

# Not sure " Exponentiated "  is the right word, but it works.
It took me 5 minutes to do it. I'm trying to do the same thing using strings/text.
This is what it looks like:
def cat_names( str1, str2, str3 ):

     catName1 = input( "What was your first cats name:\n" )
    
     catName2 = input( "What was your second cats name:\n" )
    
     catName3 = input( "What was your third cats name:\n" )

     names = catName1, catName2, catName3

     return names
   
print( "My first three cats were named:", str1 , str2, "and", str3 )

cat_names( str1 , str2, str3 )
I had no problem with the math function, but I've been playing with this one for a week and have no idea why I get this error message:

Traceback (most recent call last):
  File "/home/Tofif/Documents/save to solve/cats.py", line 20, in <module>
    print( "My First 3 cats were named:", catName1,  catName2,  catName3 )
NameError: name 'catName1' is not defined
Isn't catName1 defined in the user input request? I'm being totally blindsidded by this, and have no idea how to solve it. This was an example in my book, and this is what was stated:

{python} def myfuncttion( parameter1, parameter2 ):
return answer1, answer2, answer3
variable1, variable2, variable3 = myfunction(argument1, argument2)

I would like to know how to enter the cats names and get this function running.
I've tried running this thing about 20 different ways with different variable names and positions in the code.
I would just kike to learn how to do this, If i'm being a pain it probably isn't that important.
Thank you tofif
Reply
#2
Hi,

first, the error message does not match your code. Yes, you'll get an NameError, but it will say that str1 is not known. When asking for help, please make sure you post the error message matching your code. Everything else in confusing.

On your problem: Code in the main body (=everything not inside a function or class) is executed top to bottom, line by line. So in your 2nd code block line 13 is executed frist and than line 15. But in line 15 you do the variable assignment, thus calling the variables in line 13 won't work, as nothing is assigned there.

For learning. I highly recommend to use the official Python tutorial at docs.python.org. It's the best resource for beginners. And it's free - free to use and free from mistakes. The latter unfortunately does not apply to many books.

Regards, noisefloor
Reply
#3
You indeed have 2 different versions of the code here, and the key is the print statement. The print statement is referencing variables declared inside the function. Those variables vanish upon exit from the function. The issue is the variable scope - that's the topic to read up on.
Reply
#4
(Jun-22-2019, 06:23 PM)tofif Wrote: This was an example in my book, and this is what was stated:

 
def myfuncttion( parameter1, parameter2 ):
    return answer1, answer2, answer3
variable1, variable2, variable3 = myfunction(argument1, argument2)

Some add-on information to noisefloor and jefsummers comments.

For starters I suggest to learn meaning of parameters and arguments. Start with Python official FAQ What is the difference between arguments and parameters?

This function example (if it is full code) doesn't make sense. Function is defined with parameters but they are not used in function body.

Assigning names to function result however is quite standard practice and it's called unpacking. Function returns tuple and you can 'unpack' it by assigning names:

>>> def my_func():
...     return 1, 2, 3
...
>>> type(my_func())
tuple
>>> one, two, three = my_func()
>>> one
1
>>> two
2
>>> three
3
You can make cat_names function to work in several ways. One of them is:

>>> def cat_names():                                         # you don't define parameters                     
...    first = input('What was your first cats name: \n') 
...    second = input('What was your second cats name: \n') 
...    third = input('What was your third cats name: \n') 
...    return first, second, third                           # returns tuple with cat names
...                                                                        
>>> cat_one, cat_two, cat_three = cat_name()                 # unpack cat names, no arguments given                                   
What was your first cats name: 
A
What was your second cats name: 
B
What was your third cats name: 
C
>>> cat_one
A
>>> cat_two
B
>>> cat_three
C
>>> print('My first three cats were named: {one}, {two} and {three}'.format(one=cat_one, two=cat_two, three=cat_three))                        
My first three cats were named: A, B and C
I used .format method as you use Python 3.5. I suggest to upgrade your Python version to take advantage of f-strings. You probably observe that there is lot of repeated typing in questions, you can get rid of that but this probably not the topic while learning fundamentals.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation NameError: name 'score' is not defined - Help? MrKnd94 13 4,532 Mar-06-2023, 10:54 PM
Last Post: deanhystad
  How to correct the NameError: name 'xx' is not defined? vokoyo 5 11,287 Feb-17-2021, 05:55 AM
Last Post: delonbest
  NameError: name 'os' is not defined, & load_files(sys.argv[1]) AryaIC 3 4,690 Nov-07-2020, 07:45 PM
Last Post: jefsummers
  Error in code NameError: name ' ' is not defined ppman00 11 6,213 Sep-18-2020, 05:22 AM
Last Post: ndc85430
  NameError: name 'print_string' is not defined jamie_01 2 2,032 Jun-11-2020, 05:27 AM
Last Post: buran
  NameError x not defined Bruizeh 5 5,262 Feb-27-2019, 10:59 AM
Last Post: Larz60+
  NameError: name 'mailbox_list' is not defined pythonnewb 2 4,736 Aug-06-2017, 09:31 PM
Last Post: pythonnewb

Forum Jump:

User Panel Messages

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