0

When naming an oject with user defined attributes it says the name of object (NameError) is not used ..

Here is the part that creates an object with user defined attributes from a class called 'Dog':

def menu():
print('1. Register a new dog')
print('2. Ammend dog deatils')
print('3. Remove dog')
choice = int(input('>> '))
if choice == 1:
    Name = input('Name >> ').title()
    Age = int(input('Age >> '))
    Breed = input('Breed >> ').title()
    Energy = int(input('Energy >> '))
    Hunger = int(input('Hunger >> '))
    dog2 = Dog(Name, Age, Breed, Energy, Hunger) <--- ***HERE IS THE PROBLEM***

Error Message:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'd2' is not defined

Line 1 of my code is just the class definition :

class Dog:

    def __init__(self, n, a, b, e, h):
        self.name = n.title()
        self.age = a
        self.breed = b.title()
        self.energy = e
        self.hunger = h

Relativley new to OOP so any help is much appreciated.

PS. I want to create an object but with a user defined name eg:

#user defined name = Dog(XXX,YYY)

  • 4
    Are you accidentally using Python 2, by any chance? – kaya3 Mar 03 '20 at 22:34
  • 2
    The traceback says `` so it's not line 1 from your code but what you *enter*. @kaya3 must be correct – you *type* `d2` into one of the entry fields. – Jongware Mar 03 '20 at 22:39
  • I added a __str__ method to Class Dog and invoked print() on the newly instantiated Class Dog instance in menu(). As expected, It worked on Python 3.8.1 (even when "d2" is entered as the name). kaya3 is probably correct about Python version. – Scott Mar 03 '20 at 23:09

0 Answers0