3 Replies - 111 Views - Last Post: 10 December 2019 - 09:45 AM Rate Topic: -----

#1 saar ah   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 20-October 19

Can I get feedback on my code in python?

Posted 06 December 2019 - 09:35 PM

class Animal(object):
    
    def __init__(self,name,hunger=0):
        self.hunger=hunger
        self.name=name
        
    def Get_hunger(self):
        return self.hunger
    
    def Feed (self):
        self.hunger-=1
    
    def Get_name (self):
        return self.name 

    def  Is_hungry (self):
        if self.hunger>0:
            return True
        else:
            return False
            
    def Talk (self):
        return self.bark

class Dog(Animal):
    def self_talk(self):
        return "bark bark bark"
    def fetch_stick(self):
        return "There you go, sir!"
    
class Cat(Animal):
     def self_talk(self):
        return "meow"

     def chase_laser(self):
        return "Meeeeow"

class Skunk(Animal):
    def self_talk(self):
        return "tsssss"
    
    def stink(self):
        return "Dear lord!"

class Unicorn(Animal):
    def self_talk(self):
        return "Good day, darling"
    
    def sing(self):
        return "Iím not your toy..."

class Dragon(Animal):
    def self_talk(Animal):
        return "Raaaawr"
    
    def breath_fire(self):
        return "$@#$#@$	"

def main():
    Brownie=Dog("Brownie",10)
    Zelda=Cat("Zelda",3)
    Stinky=Skunk("Stinky",0)
    Keith=Unicorn("Keith",7)
    Lizzy=Dragon("Lizzy",1450)
    zoo_lst=[]
    zoo_lst.append(Brownie)
    zoo_lst.append(Zelda)
    zoo_lst.append(Stinky)
    zoo_lst.append(Keith)
    zoo_lst.append(Lizzy)
    zoo_lst.get_name()
    zoo_lst=[]            

    for animal in zoo_lst:
        while animal.is_hungry()==True:
            animal.feed()
    Dragon.self_talk()

            



Is This A Good Question/Topic? 0
  • +

Replies To: Can I get feedback on my code in python?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15433
  • View blog
  • Posts: 61,842
  • Joined: 12-June 08

Re: Can I get feedback on my code in python?

Posted 07 December 2019 - 10:19 AM

Just visually - there's no way for that script to start with no call to 'main'.

Also line 71, there is no function of "get_name".

Line 72 you clear your list out which doesn't make sense given you are going to loop through it in the following lines of code.

Line 75 "is_hungry" doesn't exist as a function name. https://www.programi...ords-identifier
Line 76, "feed" doesn't exist as a function name. https://www.programi...ords-identifier

Not sure what you are doing on line 77.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11746
  • View blog
  • Posts: 19,940
  • Joined: 19-March 11

Re: Can I get feedback on my code in python?

Posted 07 December 2019 - 05:26 PM

Style-wise, function names should be in snake_case (all lowercase, underscores as separators)

Logic-wise, I see that leading up to line 72 you add a bunch of stuff to a list, then in line 72 you re-initiallize that list to be empty again, so the loop starting on line 74 is not going to do anything (looping over an empty list, kind of like saying "for each honest decent Republican in the Senate, do x, y, and z" - there ain't no such animal, so nothing's going to happen)
Was This Post Helpful? 0
  • +
  • -

#4 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 562
  • View blog
  • Posts: 1,798
  • Joined: 27-December 13

Re: Can I get feedback on my code in python?

Posted 10 December 2019 - 09:45 AM

You call each animal with a name and a hunger value, e.g.Brownie=Dog("Brownie",10).
But you have no def __init__(...) function in the individual classes and no call to super() - this you need to rectify.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1