0% found this document useful (0 votes)
8 views5 pages

PYTHON Practice 1

The document outlines several Python programming exercises, including converting user input to lowercase, replacing spaces with ellipses, calculating energy from mass using Einstein's formula, and implementing a tip calculator. Each exercise includes hints and guidelines for function implementation. The document emphasizes the use of built-in Python functions and methods for string manipulation and conversion.

Uploaded by

osman.pinnacle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

PYTHON Practice 1

The document outlines several Python programming exercises, including converting user input to lowercase, replacing spaces with ellipses, calculating energy from mass using Einstein's formula, and implementing a tip calculator. Each exercise includes hints and guidelines for function implementation. The document emphasizes the use of built-in Python functions and methods for string manipulation and conversion.

Uploaded by

osman.pinnacle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Indoor Voice

WRITING IN ALL CAPS IS LIKE YELLING.

Best to use your “indoor voice” sometimes, writing entirely in lowercase.

In a file called indoor.py, implement a program in Python that prompts the user for input
and then outputs that same input in lowercase. Punctuation and whitespace should be
outputted unchanged. You’re welcome, but not required, to prompt the user explicitly, as by
passing a str of your own as an argument to input.

Hints

• Recall that input returns a str, per docs.python.org/3/library/functions.html#input.


• Recall that a str comes with quite a few methods, per
docs.python.org/3/library/stdtypes.html#string-methods.
Playback Speed
Some people have a habit of lecturing speaking rather quickly, and it’d be nice to slow them
down, a la YouTube’s 0.75 playback speed, or even by having them pause between words.

In a file called playback.py, implement a program in Python that prompts the user for input
and then outputs that same input, replacing each space with ... (i.e., three periods).

Hints

• Recall that input returns a str, per docs.python.org/3/library/functions.html#input.


• Recall that a str comes with quite a few methods, per
docs.python.org/3/library/stdtypes.html#string-methods.
Einstein
Even if you haven’t studied physics (recently or ever!), you might have heard that

, wherein represents energy (measured in Joules), represents mass (measured in kilograms), and

represents the speed of light (measured approximately as 300000000 meters per second), per
Albert Einstein et al. Essentially, the formula means that mass and energy are equivalent.

In a file called einstein.py, implement 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.

Hints

• Recall that input returns a str, per docs.python.org/3/library/func�ons.html#input.


• Recall that int can convert a str to an int, per
docs.python.org/3/library/func�ons.html#int.
• Recall that Python comes with several built-in func�ons, per
docs.python.org/3/library/func�ons.html.
Tip Calculator
And now for my Wizard tip calculator.

— Morty Seinfeld

In the United States, it’s 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 (formated 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 (formated 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.

You might also like