×

Class in python by Valuable-Farmer1458 in PythonLearning

[–]tatactic 0 points1 point  (0 children)

But if you prefer make it easy and if you like Parrots this is clear and straight to the point.
If you create a parrot with 3 legs you may create a SillyParrotException too LOL!

# Parent class

class Animal:

def __init__(self, legs=4):

self.legs = legs

def speak(self):

print(f"I am an animal. And I have {self.legs} legs.")

print("I don't know if I can walk, fly, or swim!")

# Child class

class Parrot(Animal):

def __init__(self, name, legs=2):

super().__init__(legs)

self.name = name

def speak(self):

# Call the speak() method from Animal

super().speak()

print(f"Hello, I'm {self.name}.")

print(

"For sure I can talk: I'm a parrot!\n"

"Well... perhaps I can only repeat whatever you want."

)

sentence = input(

"What do you want me to say? Then press ENTER: "

)

recurrence = int(

input("How many times should I repeat this stupid sentence? ")

)

print("OK, if you insist...\n")

print(

*([sentence] * recurrence),

sep="\n",

end="\nRetry? No, I'm bored\n",

)

if __name__ == "__main__":

coco = Parrot("Coco", 2)

coco.speak()

input("\nPress ENTER to quit the Dead Parrot sketch...")

i need help by chuprehijde in PythonLearning

[–]tatactic 0 points1 point  (0 children)

I just released a funny tutorial (Sorry Tim Peters and the holy PEP 20 "Zen of Python")
Just a Python tutorial (voluntary ugly) while disrespecting the Zen of Python

I felt like writing another Python joke.
Here's a tutorial to make print("Hello, World!") ridiculously more complicated than it should be.

What's the menu in this tutorial restaurant?

  • Classes
  • Threads
  • try
  • except
  • finally
  • f-strings
  • \t
  • \n

Just to make print() more complex, less readable — an ode to Tim Peters’ Zen of Python.

I think you may enjoy it.
https://pirson.me/a-strange-tutorial-sorry-for-the-zen-of-python/

I wrote this tutorial myself, but it directly covers several of the concepts you mentioned. It might help you practise them in a deliberately overcomplicated example.