3 Beginner-friendly Python Projects
Start of 30 days of Python tutorials
My goal for the upcoming 30 days is to publish one Python story each day. Starting today, January 23, 2024, and concluding on February 23, I’ll be sharing one Python project per day.
We will begin with creating three beginner-friendly Python projects inspired by:
Dice-rolling Simulator
Our series kicks off with a dice-rolling simulator as the first project.
To make the project more fun, I’ve decided to integrate ASCII art (inspiration drawn from the ASCII Art Archive).
"""Dice rolling simulator."""
import os
import random
import time
rolling_dice = (
[
" .-------.",
" / o /|",
"/_______/o|",
"| o | |",
"| o |o/",
"| o |/ ",
"'-------' ",
],
[
" ______ ",
" /\\ \\ ",
" /o \\ o \\ ",
"/ o\\_____\\",
"\\o /o /",
" \\ o/ o / ",
" \\/____o/ ",
],
)
dice_face = {
1: [
"._______.",
"| |",
"| o |",
"| |",
"'-------'",
],
2: [
"._______.",
"| o |",
"| |",
"| o |",
"'-------'",
],
3: [
"._______.",
"| o |",
"| o |",
"| o |",
"'-------'",
],
4: [
"._______.",
"| o o |",
"| |",
"| o o |",
"'-------'",
],
5: [
"._______.",
"| o o |",
"| o |",
"| o o |",
"'-------'",
],
6: [
"._______.",
"| o o |",
"| o o |",
"| o o |",
"'-------'",
],
}
roll = random.randint(1, 6)
for idx in range(1, 10):
if idx % 2 == 1:
dice = rolling_dice[0]
else:
dice = rolling_dice[1]
for line in dice:
print(line)
time.sleep(0.2)
# Clear console.
os.system("clear" if os.name == "posix" else "cls")
for line in dice_face[roll]:
print(line)
Running the program is surprisingly fun!
Ultimately, as the dice completes its roll:
$ python dice_rolling_simulator.py
._______.
| o o |
| |
| o o |
'-------'
Guess the Number
For our next project, we have Guess the Number:
"""Guess the number."""
import random
secret_number = random.randint(1, 100)
ATTEMPTS = 0
print("Welcome to the Guess the Number Game!")
print("I'm thinking of a number between 1 and 100.")
while True:
try:
user_guess = int(input("Enter your guess: "))
ATTEMPTS += 1
if user_guess < secret_number:
print("Too low! Try again.")
elif user_guess > secret_number:
print("Too high! Try again.")
else:
print(
f"Congratulations! You guessed the number {secret_number} "
f"in {ATTEMPTS} attempts."
)
break
except ValueError:
print("Invalid input. Please enter a valid number.")
Running it:
$ python guess_the_number.py
Welcome to the Guess the Number Game!
I'm thinking of a number between 1 and 100.
Enter your guess: 123
Too high! Try again.
Enter your guess:
Mad Libs Generator
Here’s my version of the Mad Libs Generator. In this twist, the user kicks off a story, and the computer completes it. In this iteration, I’ve employed the GPT2 model from Hugging Face (more details at Hugging Face’s GPT2).
Interestingly, this project requires the least amount of code among the ones I’ve tackled so far:
"""Mad libs generator."""
# pylint: disable=E0401
from transformers import GPT2LMHeadModel, GPT2Tokenizer, pipeline
MODEL_NAME = "gpt2"
model = GPT2LMHeadModel.from_pretrained(MODEL_NAME)
tokenizer = GPT2Tokenizer.from_pretrained(MODEL_NAME)
user_input = input("Enter prompt: ")
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
story = generator(user_input, max_length=100)
print(f"Story: {story[0]['generated_text']}")
Running it:
$ python mad_libs_generator.py
Enter prompt: There once was a cat
Story: There once was a cat that seemed to run on the sidewalk for hours.
Today it is a cat, not a dog. As in, it has to look like, well, the same
cat that was sitting at one end. And the second time it did, it looked
just like her; I found all of her bones missing and I asked, okay, what
did she look like? And it looked like like a cat that would be out of shape.
What’s Next
Tomorrow’s plan includes developing a simple calculator. You can find all the code for these projects at Python Projects on GitHub.
Further Reading
If you want to learn more about programming and, specifically, Python and Java, see the following course:
Note: If you use my links to order, I’ll get a small kickback. So, if you’re inclined to order anything, feel free to click above.