3- ๐ŸŒŸ Pykid Functions ๐ŸŒŸ

3- ๐ŸŒŸ Pykid Functions ๐ŸŒŸ


3 min read

Think of a function as a super-cool gadget that can do a special job for you, just like a robot helper!

Let's create a gadget called "my_function":

def my_function(): # Inside the gadget, we write the instructions for its job:
     print("๐Ÿ‘‹ Hello from Pykid!")

Now, we can use the gadget by calling its name, like pressing a button:

my_function() # This makes the gadget do its job, and we see the message! 
# Output: ๐Ÿ‘‹ Hello from PyKid!

We can make our gadgets even more awesome by giving them special tools to work with!

For example, let's give a gadget a name to use in a greeting:

def greet_friend(name): # Now the gadget knows to use the name we give it: 
     print(f" Hello, {name}! How are you today?")

Let's try it out!

# Output: Hello, Aisha! How are you today? 
# Output: Hello, Anas! How are you today?

Cool, right? The gadget can do different things depending on the tools we give it! It's like having a whole toolbox of gadgets!

Remember, functions are like our trusty robot helpers in coding, making our programs easier and more fun to build!

Creating a Function to Introduce Animals:

  • Imagine a special machine called "animal_sounds" that knows about different animals and their sounds.

  • This machine takes the name of an animal as its input.

  • It has a set of instructions for each animal it knows:

    • If you tell it "cat", it prints " A cat says: Meow!"

    • If you tell it "dog", it prints " A dog says: Woof!"

    • And so on for cows, ducks, and more!

  • If you give it an animal it doesn't know, it politely says "Sorry, I don't know that animal!"

  •   def animal_sounds(animal):
          if animal == 'cat':
              print("๐Ÿฑ A cat says: Meow!")
          elif animal == 'dog':
              print("๐Ÿถ A dog says: Woof!")
          elif animal == 'cow':
              print("๐Ÿฎ A cow says: Moo!")
          elif animal == 'duck':
              print("๐Ÿฆ† A duck says: Quack!")
              print("Sorry, I don't know that animal!")
      # Introducing different animals and their sounds
      # Output: ๐Ÿฑ A cat says: Meow!
      # Output: ๐Ÿถ A dog says: Woof!
      # Output: ๐Ÿฎ A cow says: Moo!
      # Output: ๐Ÿฆ† A duck says: Quack!
      # Output: Sorry, I don't know that animal!

    Playing Animal Sounds:

    • We can make this machine even cooler by giving it the ability to play actual animal sounds!

    • We import a special tool called "playsound" that can play audio files.

    • Now, instead of just printing the sound, the machine can play it out loud!

    • We provide the paths to the animal sound files, like a map for the machine to find the right sounds.

Calling the Function:

  • To use the machine, we call it by its name, "animal_sounds", and give it the name of the animal we want to hear.

  • For example, "animal_sounds('cat')" will make it play the cat's meow.

  • We can call it multiple times to hear different animals!

from playsound import playsound

# Function to play animal sounds
def animal_sounds(animal):
    if animal == 'cat':
    elif animal == 'dog':
    elif animal == 'cow':
    elif animal == 'duck':
        print("Sorry, I don't know that animal's sound!")

# Calling the function to play animal sounds

Handling Unknown Animals:

# Output: Sorry, I don't know that animal's sound!

Did you find this article valuable?

Support Azad Rasul by becoming a sponsor. Any amount is appreciated!