Skip to main content

Basic Challenge Answers

Remember, these are just one way to solve each problem - there are often multiple valid approaches in programming.

Answers

Note: These solutions assume Python 3.x.

Challenge 1: Personal Information Formatter

name = input("Enter your name: ")
age = input("Enter your age: ")
favorite_color = input("Enter your favorite color: ")

print(f"Hello, my name is {name}. I am {age} years old and my favorite color is {favorite_color}.")

Challenge 2: Simple Calculator

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

# Perform the calculation based on the operator
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
# Check for division by zero
if num2 != 0:
result = num1 / num2
else:
result = "Error: Division by zero"
else:
result = "Error: Invalid operator"

# Display the result
print(f"The result is: {result}")

Challenge 3: Temperature Converter

temp = float(input("Enter the temperature: "))
unit = input("Enter the unit (C for Celsius, F for Fahrenheit): ").upper()

if unit == "C":
converted = (temp * 9/5) + 32
print(f"{temp}°C is equal to {converted:.2f}°F")
elif unit == "F":
converted = (temp - 32) * 5/9
print(f"{temp}°F is equal to {converted:.2f}°C")
else:
print("Invalid unit. Please enter C or F.")

Challenge 4: Odd or Even Checker

# Ask the user for a number
number = int(input("Enter a number: "))

# Check if the number is odd or even
# A number is even if the remainder of division by 2 is 0
if number % 2 == 0:
print(f"{number} is even.")
else:
print(f"{number} is odd.")

Challenge 5: Grocery List Manager

grocery_list = []

while True:
print("\nGrocery List Manager")
print("1. Add item")
print("2. View list")
print("3. Remove item")
print("4. Quit")

choice = input("Enter your choice (1-4): ")

if choice == "1":
item = input("Enter the item to add: ")
grocery_list.append(item)
print(f"{item} has been added to the list.")
elif choice == "2":
if grocery_list:
print("Your grocery list:")
for item in grocery_list:
print(f"- {item}")
else:
print("Your grocery list is empty.")
elif choice == "3":
item = input("Enter the item to remove: ")
if item in grocery_list:
grocery_list.remove(item)
print(f"{item} has been removed from the list.")
else:
print(f"{item} is not in the list.")
elif choice == "4":
print("Thank you for using the Grocery List Manager. Goodbye!")
break
else:
print("Invalid choice. Please try again.")

Challenge 6: Number Guessing Game

import random

# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
attempts = 0

print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")

while True:
guess = int(input("Take a guess: "))
attempts += 1

if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the number in {attempts} attempts!")
break

Challenge 7: Word Reverser

def reverse_word(word):
return word[::-1]

user_word = input("Enter a word to reverse: ")
reversed_word = reverse_word(user_word)
print(f"The reversed word is: {reversed_word}")

Challenge 8: Tip Calculator

def calculate_tip(bill_amount, tip_percentage):
return bill_amount * (tip_percentage / 100)

bill = float(input("Enter the bill amount: $"))
tip_percent = float(input("Enter the tip percentage: "))

tip_amount = calculate_tip(bill, tip_percent)
total_bill = bill + tip_amount

print(f"Tip amount: ${tip_amount:.2f}")
print(f"Total bill: ${total_bill:.2f}")

Challenge 9: Rock, Paper, Scissors Game

import random

choices = ["rock", "paper", "scissors"]
user_score = 0
computer_score = 0

while True:
user_choice = input("Enter rock, paper, or scissors (or 'quit' to end): ").lower()
if user_choice == 'quit':
break

if user_choice not in choices:
print("Invalid choice. Please try again.")
continue

computer_choice = random.choice(choices)
print(f"Computer chose {computer_choice}")

if user_choice == computer_choice:
print("It's a tie!")
elif (user_choice == "rock" and computer_choice == "scissors") or \
(user_choice == "paper" and computer_choice == "rock") or \
(user_choice == "scissors" and computer_choice == "paper"):
print("You win!")
user_score += 1
else:
print("Computer wins!")
computer_score += 1

print(f"Score - You: {user_score}, Computer: {computer_score}")

print("Thanks for playing!")

Challenge 10: Password Strength Checker

import re

def check_password_strength(password):
# Check length
if len(password) < 8:
return "Weak: Password should be at least 8 characters long."

# Check for uppercase, lowercase, digit, and special character
if not re.search(r'[A-Z]', password):
return "Weak: Password should contain at least one uppercase letter."
if not re.search(r'[a-z]', password):
return "Weak: Password should contain at least one lowercase letter."
if not re.search(r'\d', password):
return "Weak: Password should contain at least one digit."
if not re.search(r'[!@#$%^&*(),.?":{}|<>]', password):
return "Weak: Password should contain at least one special character."

return "Strong: Password meets all criteria."

user_password = input("Enter a password to check its strength: ")
strength_result = check_password_strength(user_password)
print(strength_result)

These solutions provide a starting point for each challenge. Remember, there are often multiple ways to solve a problem in programming. As you become more comfortable with Python, you might find different or more efficient ways to approach these challenges. Keep practicing and experimenting with your code! Good luck!