Introduction to Python
Assignments for Week 1
Assignment 1
Create a Python program that can help slow down fast speakers by formatting their input text in a way that introduces pauses. This is inspired by the 0.75x playback speed feature of YouTube. Your program will prompt the user for a string of text and then output the text with each space replaced by three periods (…), creating a visual pause between words.
- Prompt for Input: Use Python’s input function to ask the user to enter some text. Save this in a variable.
- Replace Spaces: Use the replace method of strings to replace each space (” “) in the input with three periods (“…”).
- Show Output: Output the text on the screen.
Your program should work like the following video:
Assignment 2
Create a Python program that can convert text-based emoticons into their respective Unicode emoji equivalents. Specifically, : ) should be converted to 🙂 (slightly smiling face) and : ( should be converted to 🙁 (slightly frowning face). The program will prompt the user for input, process the input to make the conversions, and then print the result.
- Create faces.py: Define a convert function to replace : ) with 🙂 and : ( with 🙁.
- Implement main function: Prompt user input, call convert on it, and print the result.
- Run the script: Ensure main is called when the script is executed.
Your program should work like the following video:
Assignment No. 03
Create a program in Python that prompts the user for mass as an integer (in kilograms) and then outputs the equivalent number of Joules as an integer. Assume that the user will input an integer.
Create the file: Name the file `einstein.py`.
Prompt for input: Implement a program that asks the user for mass as an integer in kilograms.
Calculate Joules: Use the formula $(E = mc^2)$ with $(c = 3 \times 10^8) m/s$ to compute energy.
Output the result: Print the energy in Joules as an integer.
Your program should work like the following video:
Assignment 4
It is customary to leave a tip for your server after dining in a restaurant, typically an amount equal to 15% or more of your meal’s cost. Not to worry, though, we’ve written a tip calculator for you, below!
def main(): dollars = dollars_to_float(input("How much was the meal? ")) percent = percent_to_float(input("What percentage would you like to tip? ")) tip = dollars * percent print(f"Leave ${tip:.2f}") def dollars_to_float(d): # TODO def percent_to_float(p): # TODO main()
Well, we’ve written most of a tip calculator for you. Unfortunately, we didn’t have time to implement two functions:
- dollars_to_float, which should accept a str as input (formatted as \$##.##, wherein each # is a decimal digit), remove the leading \$, and return the amount as a float. For instance, given $50.00 as input, it should return 50.0.
- percent_to_float, which should accept a str as input (formatted as ##%, wherein each # is a decimal digit), remove the trailing %, and return the percentage as a float. For instance, given 15% as input, it should return 0.15.
Assume that the user will input values in the expected formats.
Hints
- Recall that input returns a str, per docs.python.org/3/library/functions.html#input.
- Recall that float can convert a str to a float, per docs.python.org/3/library/functions.html#float.
- Recall that a str comes with quite a few methods, per docs.python.org/3/library/stdtypes.html#string-methods.
Your program should work like the following video: