DEV Community

Cover image for Day 05 of My AI & Data Mastery Journey: From Python to Generative AI
Nitin-bhatt46
Nitin-bhatt46

Posted on

Day 05 of My AI & Data Mastery Journey: From Python to Generative AI

Functions :-

Project :- **
**Tiny functions tip + username

Define a function to create a username:

Remove spaces from the given first name

Combine it with an underscore and the birth year

Define a function to calculate the total bill with tip:

Multiply the bill by the tip percentage (divide the percentage by 100)

Add this amount to the original bill

Define a function to split the total amount:

Divide the total by the number of people

Start the main program:

Ask the user to enter their first name

Ask the user to enter their birth year

Ask the user to enter the bill amount

Ask the user to enter the tip percentage

Ask the user to enter the number of people to split the bill

Use the username creation function to get the username

Use the total calculation function to find the total bill

Use the split function to find how much each person pays

Display the username

Display the total bill (rounded to two decimal places)

Display how much each person has to pay (rounded to two decimal places)

Creation 1 — Wrap Your Tip Project Into Functions (small)

Take your working tip calculator from earlier and refactor it into functions exactly like the tiny template above. Requirements:

Use get_username, calculate_total, split_amount.

Validate people >= 1. If invalid, print a friendly message and stop.

Format outputs to 2 decimal places.

Solution :- link in bio Github

Creation 2 — Mini Treasure Function

Create a very small function play_round(choice) that:

accepts a string ("left" or "right") and returns "win" or "lose" or "wait" depending on a tiny rule you decide (keep it simple).

Then write a main() that asks the user (one input), calls play_round, and prints a friendly message using the returned value.

Example rule (use this or make your own):

left → "continue"

right → "fall" → map to lose

If input not recognized → "invalid"

Why: teaches returning values and using them.

Solution:- link in bio Github

Debugging 1 — Forgot return (very common)

Buggy snippet:

`def calculate_total(bill, tip):
  total = bill + bill * tip / 100
  # missing return

t = calculate_total(100, 10)
print(t)`

Fix: add return total.
Explain: without return the function gives None.

Solution :- link in bio Github

Debugging 2 — Type conversion inside functions

Buggy snippet:

`def split_amount(total, people):
  return total / people

bill = input("Bill: ")
people = input("People: ")
t = float(bill) + 0.0
print(split_amount(t, people))`

Problems: people is a string → division errors.
Fix idea: convert people = int(input(...)) or convert inside call: split_amount(t, int(people)). Add try/except if you want extra safety.

Solution :- link in bio Github

Thank you Day 05 is Completed.

Top comments (0)