Oop program- I have tried for days- HELP

This is the place for queries that don't fit in any of the other categories.

Oop program- I have tried for days- HELP

Postby bewareofuser » Sat Jul 06, 2013 8:08 pm

I am new to programming and have taken a summer college class-
I have been bustiong my butt but could use some help
this oop program I cant seem to get it right
can someone take a look at it and in simple terms help me get it to call INFO from the class

Code: Select all
class petinfo:
     def __init__(self,age,name,type1):
                 self.class_age=age
                 self.class_name=name
                 self.class_type1=type1
    def getname(self):
        return self.class_name
    def getage(self):
        return self.age
    def gettype(self):
        return self.type1
#main
age=0.0
name= ""
type1 = ""
name= (input("Please enter your pets name"))
type1=(input("Please enter the type of animal your pet is"))
age= float(input("Please enter your pets age"))
petinfo = pet1
print (pet1.name())
print (pet1.type())
print (pet1.age())
Last edited by micseydel on Sat Jul 06, 2013 8:34 pm, edited 2 times in total.
Reason: Locked post.
bewareofuser
 
Posts: 8
Joined: Sat Jul 06, 2013 8:01 pm

Re: Oop program- I have tried for days- HELP

Postby micseydel » Sat Jul 06, 2013 8:35 pm

bewareofuser wrote:help me get it to call INFO from the class

I have no idea what you mean by that. Could you elaborate? It would be helpful if you posted your exact assignment here.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 929
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Oop program- I have tried for days- HELP

Postby Yoriz » Sat Jul 06, 2013 8:38 pm

Your use of classes is all wrong, studdy this version of your code and compare it to yours.
Code: Select all
class PetInfo:
    def __init__(self, age, name, type1):
        self.age = age
        self.name = name
        self.type1 = type1

# main
name = (input("Please enter your pets name"))
type1 = (input("Please enter the type of animal your pet is"))
age = float(input("Please enter your pets age"))
pet1 = PetInfo(age, name, type1)
print(pet1.name)
print(pet1.type1)
print(pet1.age)
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 566
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Oop program- I have tried for days- HELP

Postby micseydel » Sat Jul 06, 2013 8:43 pm

I just noticed that you have some egregious errors in your code. You need to revisit a basic Python tutorial, and once you're comfortable with it then attempt OOP, which is an advanced topic in programming. Also, don't post again without reading this.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 929
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Oop program- I have tried for days- HELP

Postby bewareofuser » Sat Jul 06, 2013 9:19 pm

This was the assignment
Design a class named Pet, which should have the following fields:
Name: the name field holds the name of the pet.
Type: the type field holds the type of animal that a pet is. Example values are “Dog”, “Cat”, and “Bird”.
Age: the age field holds the pet’s age.
The pet class should also have the following methods:
Constructor: pet method.
getName: the getName method returns the value of the name field.
getType: the getType method returns the value of the type field.
getAge: the getAge method returns the value of the age field.
Once you have designed the class, design a program that creates an object of the class and prompts the user to enter the name, type, and age of his or her pet. Use the object’s methods to retrieve the pet’s name, type and age and display this data on the screen.
bewareofuser
 
Posts: 8
Joined: Sat Jul 06, 2013 8:01 pm

Re: Oop program- I have tried for days- HELP

Postby bewareofuser » Sat Jul 06, 2013 9:21 pm

I was trying to use a constructor
as the instruction says but without any luck
Isn't this the constructor???
def getname(self):
return self.class_name
def getage(self):
return self.age
def gettype(self):
return self.type1
Last edited by bewareofuser on Sat Jul 06, 2013 9:25 pm, edited 1 time in total.
bewareofuser
 
Posts: 8
Joined: Sat Jul 06, 2013 8:01 pm

Re: Oop program- I have tried for days- HELP

Postby micseydel » Sat Jul 06, 2013 9:23 pm

micseydel wrote:I just noticed that you have some egregious errors in your code. You need to revisit a basic Python tutorial, and once you're comfortable with it then attempt OOP, which is an advanced topic in programming. Also, don't post again without reading this.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 929
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Oop program- I have tried for days- HELP

Postby bewareofuser » Sat Jul 06, 2013 10:01 pm

I am trying to figure out the errors and I have read the tutorial section and the posts and I still can't determine what your trying to tell me

This is the error I am receiving
Traceback (most recent call last):
File "E:/programming class/fixed lab8.py", line 20, in <module>
petinfo= pet1 (age,name,type1)
NameError: name 'pet1' is not defined
>>>

Code: Select all
class petinfo:
     def __init__(self,age,name,type1):
               
                 self.pet1_age=age
                 self.pet1_name=name
                 self.pet1_type1=type1
     def getname(self):
        return self.pet1_name
     def getage(self):
        return self.pet1_age
     def gettype(self):
        return self.pet1_type1
#main
age=0.0
name= ""
type1 = ""
name= (input("Please enter your pets name"))
type1=(input("Please enter the type of animal your pet is"))
age= float(input("Please enter your pets age"))
petinfo= pet1 (age,name,type1)
print (pet1.name())
print (pet1.type())
print (pet1.age())

If you can tell me how I am supposed to post it or what I am missing by the INFO I will do it the way you want just need to know specifically what your looking for me to do.
bewareofuser
 
Posts: 8
Joined: Sat Jul 06, 2013 8:01 pm

Re: Oop program- I have tried for days- HELP

Postby micseydel » Sat Jul 06, 2013 10:12 pm

The link I posted says to include the traceback, among other things. The traceback is the most important for now. You should read it and post what else, but I'm not going to repeat what's already there.

The traceback says that the line
Code: Select all
petinfo= pet1 (age,name,type1)

causes a NameError. What do you expect this line to do? (Usually we ask "what does it really do, but right now you hopefully see it causes an error in the program.) Do you know what a NameError is? Have you Googled it, if not?

It is at the line I discussed above that you need to focus, and ask very specific questions. Don't bother looking at or taking into consider any lines after that for now.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 929
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Oop program- I have tried for days- HELP

Postby bewareofuser » Sun Jul 07, 2013 12:28 am

I am trying to define pet1
which for some reason I cant figure it out
I have googled it but not understanding what I am doing wrong
I thought I had to declare it?
bewareofuser
 
Posts: 8
Joined: Sat Jul 06, 2013 8:01 pm

Re: Oop program- I have tried for days- HELP

Postby Yoriz » Sun Jul 07, 2013 12:51 am

When you asign something, what is on the right of = gets asigned to what is on the left
which is what you did in the following line
Code: Select all
name = (input("Please enter your pets name"))

in the line you are getting the error
Code: Select all
petinfo= pet1 (age,name,type1)

you have put them around the wrong way
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 566
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Oop program- I have tried for days- HELP

Postby bewareofuser » Sun Jul 07, 2013 12:59 am

Traceback (most recent call last):
File "E:\programming class\lab8corrections.py", line 19, in <module>
petinfo= pet1(age,name,type1)
NameError: name 'pet1' is not defined
I have done it both ways
bewareofuser
 
Posts: 8
Joined: Sat Jul 06, 2013 8:01 pm

Re: Oop program- I have tried for days- HELP

Postby Yoriz » Sun Jul 07, 2013 1:06 am

what do you mean you've done it both ways, that is still wrong they are the wrong way around
Code: Select all
petinfo= pet1(age,name,type1)

it should be like the following
Code: Select all
pet1 = petinfo(age,name,type1)


Also note class names should be CamelCase so you can tell which are classes, so petinfo should be PetInfo
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 566
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Oop program- I have tried for days- HELP

Postby bewareofuser » Sun Jul 07, 2013 1:15 am

Please enter your pets namegracie
Please enter the type of animal your pet isDog
Please enter your pets age5
Traceback (most recent call last):
File "E:\programming class\lab8corrections.py", line 19, in <module>
pet1= Petinfo(age,name,type1)
NameError: name 'Petinfo' is not defined

Please enter your pets nameGracie
Please enter the type of animal your pet isdog
Please enter your pets age5
Traceback (most recent call last):
File "E:\programming class\lab8corrections.py", line 20, in <module>
print(pet1.name())
AttributeError: 'Petinfo' object has no attribute 'name'

Code: Select all
class Petinfo:
     def __init__(self,age,name,type1):
                 self.Petinfo_age=age
                 self.Petinfo_name=name
                 self.Petinfo_type1=type1
     def getname(self):
       return self.Petinfo_name
     def getage(self):
        return self.Petinfo_age
     def gettype1(self):
        return self.Petinfo_type1
#main
age=0.0
name=""
type1=""
name= (input("Please enter your pets name"))
type1=(input("Please enter the type of animal your pet is"))
age= float(input("Please enter your pets age"))
pet1= Petinfo(age,name,type1)
print(pet1.name())
print (pet1.age())
print (pet1.type1())
bewareofuser
 
Posts: 8
Joined: Sat Jul 06, 2013 8:01 pm

Re: Oop program- I have tried for days- HELP

Postby Yoriz » Sun Jul 07, 2013 1:33 am

Ive puts comments in lines where there are errors and put a corrected line right after it.
Code: Select all
class Petinfo:
     def __init__(self,age,name,type1): # indenting is wrong should be 4 spaces
    def __init__(self, age, name, type1): # correct way
                 self.Petinfo_age=age # wrong & indenting wrong
        self.age = age # like this
                 self.Petinfo_name=name # wrong & indenting wrong
        self.name = name # like this
                 self.Petinfo_type1=type1 # wrong & indenting wrong
        self.type1 = type1 # like this
     def getname(self): # indenting wrong
    def getname(self): # like this
       return self.Petinfo_name # wrong & indenting wrong
        return self.name # like this
     def getage(self): # indenting wrong
    def getage(self) # like this
        return self.Petinfo_age # wrong
         return self.age # like this
     def gettype1(self): # indenting wrong
    def gettype1(self): # like this
        return self.Petinfo_type1 # wrong & indenting wrong
        return self.type1 # like this
#main
age=0.0 # not required
name="" # not required
type1="" # not required
name = (input("Please enter your pets name")) # ok
type1 = (input("Please enter the type of animal your pet is")) # ok
age = float(input("Please enter your pets age")) # ok
pet1 = Petinfo(age, name, type1) # ok
print(pet1.name()) # wrong they asked you to use getter
print(pet1.getname()) # like this
print (pet1.age()) # wrong they asked you to use getter
print(pet1.getage()) # like this
print (pet1.type1()) # wrong they asked you to use getter
print (pet1.gettype1()) # like this


You shouldnt need to create get methods, you should be able to just access the class attributes directly but i guess it what you've been asked to do
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 566
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Oop program- I have tried for days- HELP

Postby bewareofuser » Sun Jul 07, 2013 1:56 am

so class doesn't have to be entered in for the value?
and my spacing was wrong?
thank you so much for the help
it is greatly appreciated
bewareofuser
 
Posts: 8
Joined: Sat Jul 06, 2013 8:01 pm


Return to General Coding Help

Who is online

Users browsing this forum: Cereal and 3 guests

cron