Python Lab Exercise Made Simple for Beginners

Arzath AreeffArzath Areeff
3 min read

Exercise 1: Basic Calculator

Lab Exercise Instructions:

  1. Create a new Python file called calculator.py

  2. Implement the calculator program following these steps:

    • First, ask the user to input an operator

    • Then, ask for two numbers

    • Use conditional statements (if/elif/else) to determine which operation to perform

    • Display the result in a user-friendly format

  3. Test your program with different operators and numbers

  4. Bonus Challenges:

    • Add support for additional operations like exponentiation (**) or modulus (%)

    • Handle invalid number input (non-numeric values)

    • Allow the user to perform multiple calculations without restarting the program

Example Output:

Welcome to the Basic Calculator!
Available operations: +, -, *, /
Enter an operator (+, -, *, /): *
Enter the first number: 5
Enter the second number: 7
Result: 5.0 * 7.0 = 35.0
Calculation complete!

Exercise 2: Countdown Timer

Requirements:

  1. Ask the user to enter a time in seconds.

  2. Display the countdown in HH:MM:SS format.

  3. Print "TIME'S UP!" when the countdown reaches zero.

Bonus Challenges:

  • Add error handling for non-integer inputs.

  • Allow the user to pause/resume the timer.

  • Play a sound when the timer ends (requires winsound or playsound module).


Exercise 3: Random Number Generator

Requirements:

  1. Ask the user for the lower and upper bounds.

  2. Generate and display a random number within that range.

  3. Allow the user to generate new numbers or exit.

Bonus Challenges:

  • Add input validation (ensure upper is greater than lower).

  • Let the user specify how many random numbers to generate at once.

  • Track and display previously generated numbers.


✅Solution Code:

Exercise 1: Basic Calculator

# Basic Calculator Program

# Display welcome message
print("Welcome to the Basic Calculator!")
print("Available operations: +, -, *, /")

# Get user input
operator = input("Enter an operator (+, -, *, /): ")
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Perform calculation and display result
if operator == '+':
    result = num1 + num2
    print(f"Result: {num1} + {num2} = {result}")
elif operator == '-':
    result = num1 - num2
    print(f"Result: {num1} - {num2} = {result}")
elif operator == '*':
    result = num1 * num2
    print(f"Result: {num1} * {num2} = {result}")
elif operator == '/':
    if num2 != 0:  # Check for division by zero
        result = num1 / num2
        print(f"Result: {num1} / {num2} = {result}")
    else:
        print("Error: Cannot divide by zero!")
else:
    print("Error: Invalid operator entered!")

print("Calculation complete!")

Exercise 2: Countdown Timer

import time

# Get user input
my_time = int(input("Enter the time in seconds: "))

# Countdown loop
for x in range(my_time, -1, -1):
    seconds = x % 60
    minutes = int(x / 60) % 60
    hours = int(x / 3600)

    # Format and display time
    print(f"{hours:02}:{minutes:02}:{seconds:02}")
    time.sleep(1)  # Wait 1 second

print("TIME'S UP!")

Exercise 3: Random Number Generator

import random

print("Welcome to the Random Number Generator!")

while True:
    # Get user input
    lower = int(input("Enter the lower bound: "))
    upper = int(input("Enter the upper bound: "))

    # Generate and display random number
    random_num = random.randint(lower, upper)
    print(f"Random number: {random_num}")

    # Ask if user wants to continue
    choice = input("Generate another number? (y/n): ").lower()
    if choice != 'y':
        break

print("Goodbye!")
0
Subscribe to my newsletter

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

Written by

Arzath Areeff
Arzath Areeff

I co-founded digizen.lk to promote online safety and critical thinking. Currently, I’m developing an AI app to fight misinformation. As Founder and CEO of ideaGeek.net, I help turn startup dreams into reality, and I share tech insights and travel stories on my YouTube channels, TechNomad and Rz Omar.