Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save JorelRippon/1feec90925ea755cc4591ebad7e10f99 to your computer and use it in GitHub Desktop.

Select an option

Save JorelRippon/1feec90925ea755cc4591ebad7e10f99 to your computer and use it in GitHub Desktop.
lesson 3 homework
# Homework Lesson 3 - Strings
# READ CAREFULLY THE EXERCISE DESCRIPTION AND SOLVE IT RIGHT AFTER IT
# ---------------------------------------------------------------------
# Exercise 1: Personalized Greeting
# Write a program that takes a user's name as input
# and then greets them using an f-string: "Hello, [name]!"
#
# Example Input: "Alice"
# Example Output: "Hello, Alice!"
name = input("Enter yor name: ")
print(f"????")
name = "jorel"
print(f"hello, {name}, you're cool!")
# ---------------------------------------------------------------------
# Exercise 2: Greeting with User's Favorite Activity
# Write a program that takes a user's name and their
# favorite activity as input, and then greets them
# using the formatting method of your choice as:
# "Hello, [name]! Enjoy [activity]!"
# Example Input:
# Name: Emily
# Favorite Activity: hiking
# Example Output: "Hello, Emily! Enjoy hiking!"
name = "Jorel"
activity = "dancing"
print(f"Hello {name}! Enjoy {activity}!")
# ---------------------------------------------------------------------
# Exercise 3: Membership Cards
# You are designing a simple registration system for a club.
# When new members sign up, you want to ensure their names
# are displayed in uppercase on their membership cards.
# Write a program that takes the new member's name as
# input and prints it in uppercase and prints a welcome message
# using .format()
# Example Input:
# Name: Emily
# Example Output: "Welcome, Emily! Your name in uppercase is: EMILY!"
# Ask for the member's name
name = input("Name: ")
# Convert name to uppercase
name_upper = name.upper()
# Print the welcome message using .format()
print("Welcome, {}! Your name in uppercase is: {}!".format(name, name_upper))
# ---------------------------------------------------------------------
# Exercise 4: User Profile Creation
# Build a user profile generator. Ask
# the user for their first name, last name, and age. Create
# a profile summary using .title(), .upper(), and .format().
#
# Example Input:
# First name: john
# Last name: smith
# Age: 28
#
# Example Output:
# Name: John Smith
# Age: 28
# Ask for user information
first_name = input("First name: ")
last_name = input("Last name: ")
age = input("Age: ")
# Format the name nicely: capitalize first letter of each word
full_name = (first_name + " " + last_name).title()
# Make age a string (already is from input), no change needed
# You can use .upper() on something if you want, e.g. a welcome message
# Build and print the profile summary using .format()
print("Name: {}".format(full_name))
print("Age: {}".format(age))
# ---------------------------------------------------------------------
# Exercise 5: Text message limits
# You are developing a text messaging application that limits the
# number of characters in a single message. Your task is to create
# a Python program that takes a message as input from the user.
# The program should calculate and display the number of characters
# in the message, including spaces, and format the output using
# an f-string. This character count will help users ensure their
# messages fit within the allowed limit.
# Ask the user to enter their message
message = input("Enter your message: ")
# Count the number of characters (including spaces)
character_count = len(message)
# Display the result using an f-string
print(f"Your message has {character_count} characters (including spaces).")
# ---------------------------------------------------------------------
# Exercise 6: Text Transformation Game
# Create a text transformation game. Ask the user
# to enter a sentence. Replace all vowels with '*'. Display the
# modified sentence.
#
# Example Input: "Hello, world!"
# Example Output: "H*ll*, w*rld!"
sentence = input("Enter a sentence: ")
transformed_sentence = sentence.replace('a', '*')
# Ask the user for a sentence
sentence = input("Enter a sentence: ")
# Replace all vowels (a,e,i,o,u,A,E,I,O,U) with '*'
transformed_sentence = sentence.replace('a', '*').replace('e', '*').replace('i', '*').replace('o', '*').replace('u', '*') \
.replace('A', '*').replace('E', '*').replace('I', '*').replace('O', '*').replace('U', '*')
# Show the result
print("Transformed sentence:", transformed_sentence)
# ------------------------------# ---------------------------------------------------------------------
# Exercise 7: Extracting Information
# The variable 'data' is a student record in the format "name:age"
# Use string slicing and string methods to extract the name and the age
# and print the result formatted.
#
# data = "lucy smith:28"
#
# Expected output:
# Name: Lucy Smith
# Age: 28
data = "lucy smith:28"
# Split the string at the colon into a list: ["lucy smith", "28"]
parts = data.split(":")
# Get name and age from the list
name = parts[0].strip().title() # strip removes extra spaces, title capitalizes
age = parts[1].strip() # age is already clean
print("Name:", name)
print("Age:", age)
# ---------------------------------------------------------------------
# Exercise 8: Miles to Kilometers Conversion
# Write a program that converts a distance in miles to kilometers.
# Take the distance in miles as input, convert it to kilometers
# using the formula miles * 1.6, and display the
# result using f-strings.
# Example Input: 10
# Example Output: 10 miles is approximately 16.0 kilometers.
# We are converting the input string to float:
# Input: float("1.23")
# Output: 1.23
miles = float(input("Enter distance in miles: "))
# Ask the user for distance in miles
miles = float(input("Enter distance in miles: "))
# Convert miles to kilometers (1 mile ≈ 1.60934 km, but exercise uses 1.6)
kilometers = miles * 1.6
# Display the result using an f-string
print(f"{miles} miles is approximately {kilometers} kilometers.")
# ---------------------------------------------------------------------
# Exercise 9: Workouts calculator
# Write a Python program that asks the user to input the number
# of minutes spent on three different exercises: cardio, strength
# training, and yoga using the input() function. Convert the input
# strings to integers using the int() function. Calculate the
# total time spent on workouts by summing up the minutes from all
# three activities. Based on the total workout time, provide a
# motivational message using an f-string that encourages the user
# to stay consistent and reach their fitness goals. Display the
# motivational message to the user.
# Ask the user for minutes spent on each exercise
cardio_minutes = int(input("Enter minutes spent on cardio: "))
strength_minutes = int(input("Enter minutes spent on strength training: "))
yoga_minutes = int(input("Enter minutes spent on yoga: "))
# Calculate total workout time
total_minutes = cardio_minutes + strength_minutes + yoga_minutes
# Motivational message using an f-string
print(f"Great job! You worked out for a total of {total_minutes} minutes today.")
print(f"Keep showing up — every minute brings you closer to your fitness goals! 💪")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment