DEV Community

Cover image for 🐴 The Great Horse Racing Competition of Necoz Kingdom! | Python Modules πŸ“¦
Sᴛᴀʀ⭐ for Necoz

Posted on AI-assisted

🐴 The Great Horse Racing Competition of Necoz Kingdom! | Python Modules πŸ“¦

On a bright sunny morning the King of Necoz declared a horse racing contest!
(The deer is in the cover image, sorry, it's not a horse. πŸ˜…)

πŸ‘‘ King: The winner will get 1,000 gold coins!

Tom Jerry and Luna hurriedly signed up for the race. But the King had an issue. Hundreds of people wished to participate and managing everything was becoming a bit tricky.

Just then the royal programmer arrived.

🐱 Programmer: Don't fret Your Majesty! Here's my magic Python Modules trick!

Tom: What are Modules?

Programmer: Think of a box filled with useful tools. Python Modules are much the same! They are Python files with reusable codes like functions variables and classes.

He took out his laptop.

Programmer: Let's pick a random horse number!

python

import random

horse = random.randint(1, 10)

print(horse)
Enter fullscreen mode Exit fullscreen mode

Output:

text

7
Enter fullscreen mode Exit fullscreen mode

Jerry: WOW! How did you do that?

Programmer: I used the random module. Python already comes with modules like random math and datetime. These are called Built-in Modules.

Luna: Do we have the ability to create our own modules?

Programmer: Sure! Let's create a module called horse.py.

File: horse.py

python

def winner():
    print("Thunder is the winner!")
Enter fullscreen mode Exit fullscreen mode

Now in another Python file:

python

import horse

horse.winner()
Enter fullscreen mode Exit fullscreen mode

Output:

text

Thunder is the winner!
Enter fullscreen mode Exit fullscreen mode

The king couldn't stop smiling after he saw that registration process went absolutely smoothly.

πŸ‘‘ King: Great! Let's proceed with the race now!

Five horses were lined up on the starting line. Tom was on Thunder and Jerry was riding on Lightning.

πŸ”” King: Three… Two… One… GO!

All horses have started running!

Tom: C'mon Thunder! Run faster!

Suddenly, Lightning passed Thunder.

Jerry: HAHA! Seems like I am going to win!

But just before reaching the finishing line, Thunder suddenly sped up and won the race!

πŸ† King: Congratulations, Tom! You won the race!

Tom got his reward of 1,000 gold coins and a golden trophy.

🐭 Jerry: Next time, I will use my own Python module to help me win!

Everyone started laughing.

The royal programmer couldn't stop smiling and said:

Programmer: Remember! Write code once and reuse it whenever you want to. It is the power of Python Modules!

And thus, the people of Necoz celebrated their biggest horse racing competition!

Top comments (5)

Collapse
 
nyaomaru profile image
nyaomaru Necoz •

πŸ‘‘ King: Great write-up!! I've learned about Python modules, and I'm excited to see what the royal programmer will teach me next! πŸ˜ΉπŸ’š

Let the adventures of Necoz Kingdom continue! 🐈🏰

Collapse
 
leob profile image
leob Necoz •

Nice story, love it - learned something!

Collapse
 
star123 profile image
Sᴛᴀʀ⭐ Necoz •

Thanks

Collapse
 
beararmd profile image
Bear Mac •

Very creative share... thank you!

Collapse
 
star123 profile image
Sᴛᴀʀ⭐ Necoz •

Thank you 😊