← Back to Exercises
Module 1

Solutions

Compare your solutions and learn best practices

⚠️ Try Before You View!

Make sure you've attempted the exercises before viewing these solutions. Learning happens when you struggle through problems yourself!

Solution 1: Personal Information

Collect and display user information in a formatted message.

📝 Explanation

This solution uses the input() function to collect data from the user, stores it in variables, and then uses an f-string to create a nicely formatted output message.

💻 Code

# Personal Information Program
name = input("What is your name? ")
age = input("How old are you? ")
city = input("Which city do you live in? ")

# Display formatted message
print(f"\nHello! Your name is {name}.")
print(f"You are {age} years old.")
print(f"You live in {city}.")
print(f"\nNice to meet you, {name} from {city}!")

✨ Key Points

  • Use input() to get user data
  • Store data in descriptive variable names
  • Use f-strings for clean string formatting
  • \n creates a new line in output

Solution 2: Basic Calculator

Perform arithmetic operations based on user input.

📝 Explanation

This calculator takes two numbers and an operator, then uses conditional statements to perform the correct operation. It includes error handling for division by zero.

💻 Code

# Basic Calculator
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operator = input("Enter operator (+, -, *, /): ")

if operator == '+':
    result = num1 + num2
elif operator == '-':
    result = num1 - num2
elif operator == '*':
    result = num1 * num2
elif operator == '/':
    if num2 != 0:
        result = num1 / num2
    else:
        result = "Error: Division by zero!"
else:
    result = "Error: Invalid operator!"

print(f"Result: {result}")

✨ Key Points

  • Convert input to float for decimal support
  • Use if-elif-else for multiple conditions
  • Check for division by zero
  • Validate user input

Solution 3: Temperature Converter

Convert between Celsius and Fahrenheit temperatures.

📝 Explanation

This program converts temperatures in both directions using the standard conversion formulas. It provides a menu for the user to choose the conversion type.

💻 Code

# Temperature Converter
print("Temperature Converter")
print("1. Celsius to Fahrenheit")
print("2. Fahrenheit to Celsius")

choice = input("Enter choice (1 or 2): ")

if choice == '1':
    celsius = float(input("Enter temperature in Celsius: "))
    fahrenheit = (celsius * 9/5) + 32
    print(f"{celsius}°C = {fahrenheit}°F")
elif choice == '2':
    fahrenheit = float(input("Enter temperature in Fahrenheit: "))
    celsius = (fahrenheit - 32) * 5/9
    print(f"{fahrenheit}°F = {celsius}°C")
else:
    print("Invalid choice!")

✨ Key Points

  • Provide clear menu options
  • Use correct conversion formulas
  • Format output with degree symbols
  • Handle invalid input

Ready for the project?

Apply everything you've learned in the module project!

View Project