Weekend-2 # Project -3: Productivity Pet with Python

..........
5 min read

Hi,

Today I have decided to build something that gamifies consistency . A Productivity Pet ๐Ÿฆโ€๐Ÿ”ฅ โ€“A virtual pet that hatches and grows as you get things done! Each task you complete โ€˜feedsโ€™ it. It is a catchy pet that feeds on consistency and productivity.

PRODUCTIVITY PET ๐Ÿฆโ€๐Ÿ”ฅ

OUTPUT:

WHAT I PRACTISED ๐ŸŽ‰:

  • Conditional statements ( if, elif, else)

  • Loops ( while)

  • Libraries (coloroma)

  • Modules (random)

  • Basic Arithmetic Logic ( percentage calculation)

WHATโ€™S NEW ๐Ÿ‘€:

  • Random module ( random. choice)

  • List Comprehension

  • Format specifier for floating points ( .2f)

EXPLANATION:

STEP 1: SET UP AND LIBRARIES

from colorama import Fore, Style
import random
  • colorama = for colourful feature

  • random = to randomly pick items

STEP 2: WE IMPORT REQUIRED LIBRARIES (COLORAMA) AND MODULES (RANDOM)

print(Fore.BLUE + "PRODUCTIVITY PET ๐Ÿฆโ€๐Ÿ”ฅ " + Style.RESET_ALL)
print(Fore.LIGHTCYAN_EX + "Current Progress of Pet :๐Ÿฅš\nComplete your tasks to hatch your pet!" + Style.RESET_ALL)
  • Print title and intial pet stage (current progress of the โ€˜PETโ€™)

STEP 2: TASK INPUT LOGIC ( INSIDE WHILE LOOP):

while True:
    user = input("Did you complete your tasks today? ").lower().strip()
    if user in ["yes", "yeah", "yep"]:
        tasks = [input("Enter task: ") for _ in range(int(input("How many tasks in your list? ")))]
        completed = int(input("How many tasks did you complete? "))
  • To let the program to keeps asking you the same question over and over ( so that you can stop when don't want to play ) we initialize while loop, until you decide to stop.

  • while True keeps program run until user quits.

  • Inside the loop we set a condition (if, elif, else)

  • we ask user if they completed tasks. if answer is yes , a block of code is written to handle it. Elif user says no , read at STEP ___

  • if the user enters yes :

  • We ask them to enter the tasks:
tasks = [input("Enter task: ") for _ in range(int(input("How many tasks in your list? ")))]
  • The above line is a LIST COMPREHENSION.

  • List comprehension - A list comprehension is a quick way to create a list in one line by looping over items and optionally applying a transformation.

  • Basically the above line means:

  •   num_tasks = int(input("How many tasks in your list? "))
      tasks = []
      for _ in range(num_tasks):
          task = input("Enter task: ")
          tasks.append(task)
    
  • now do you get it? We use 5 lines to ask for input from users and append it to list but in list comprehension we only took a single line to implement the same thing. Donโ€™t woryy the list comprehension might look a bit jammy and hard at first, but with good practice you will understand it.

STEP 3 : CALCULATION OF PERCENTAGE ( PROGRESS):

 completed = int(input("How many tasks did you complete? "))

        percentage = (completed / len(tasks)) * 100
        print(f"Progress: {percentage:.2f}%")
  • to calculate the percentage we prompt user to enter how many tasks they completed

  • Equation : (completed/ len(tasks)) x 100

  • we will print the the answer with addition of .2f

  • .2f= .2f is a format specifier that tells Python to display a number as a floating-point with 2 digits after the decimal.

    • Without .2f = Progress: 73.4567%

    • with .2f =Progress: 73.46%

    • see the difference? .2f makes a number round to 2 decimal places for display.

STEP 4: RANDOM MODULE USAGE ( RANDOM PET SELECTION):

  petc = ["๐Ÿฆ", "๐Ÿง", "๐Ÿฆ‡", "๐Ÿฆค", "๐Ÿฆ†", "๐Ÿฆ…"]
        pets = random.choice(petc)
  • a variable petc is taken and to it, a list of different bird emojis is stored

  • random.choice() - picks one emoji at random from the list, so each time your pet grows, it might look different.

  • random.choice() -picks a random item from a list.

STEP 4: CHECKING THE PERCENTAGE TO CHECK PET PROGRESS:

if percentage < 50:
            print(Fore.YELLOW + "Oops! The pet didnโ€™t hatch. Current stage: ๐Ÿฅš" + Style.RESET_ALL)
        elif 50 <= percentage < 80:
            print(Fore.YELLOW + "CURRENT PROGRESS : ๐Ÿฃ" + Style.RESET_ALL)
        else:
            print(Fore.GREEN + f"Congratulations!\nCURRENT PROGRESS : {pets}" + Style.RESET_ALL)
  • If percentage is less than 50 - still egg

  • if percentage 50-79= hatched

  • if percentage is 80 and above- fully grown pet

STEP 6: IF USER TYPED โ€˜ NOโ€™:

  •    elif user in ["no", "nope", "nah"]:
              print("You need to complete your tasks to hatch your pet")
              cont = input("Do you want to continue? ").lower().strip()
              if cont in ["yes", "yeah", "yep"]:
                  continue
              else:
                  print("Thank you for using PRODUCTIVITY PET! Goodbye!")
                  break
    
  • if user said no ( is not productive)

    • Then it asks if you want to keep going - (do you want to continue)

      • If you say yes, the program loops back and asks about your tasks again -continue

      • If you say no, it says goodbye and stops the program - break

EXTRA :

  • I have used the โ€˜randomโ€™ module to add variety to the petโ€™s appearance. This one is optional, so you can choose to use it or not. If you want to use it, donโ€™t forget to
import random

I have provided the GitHub link below. You can also modify the code more creatively and freely to your liking:

https://github.com/log-Null/python-CLI-collections/tree/main/PRODUCTIVITY_PET

  • Gamified CLI projects can be fun and creative

  • Practicing list comprehensions not only saves time but also enhances code readability and demonstrates Python efficiency. .

  • Can be modified: with usage of file handling, adding mini-rewards etc

0
Subscribe to my newsletter

Read articles from ..... directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

.....
.....

Aspiring Data Science Engineer | CSE Final Year student| Python GenAI Enthusiast | Building & Blogging my journey