In this post I show how to solve the problems from topic 0 - Functions and Variables of CS50’s - Introduction to Python course.

Table of Contents

Indoor Voice

The first problem brings that WRITING IN ALL CAPS IS LIKE YELLING so we have to create a program to lowercase all letters that are in caps.

Here are the steps to solve this problem:

  1. First we need to take user’s input, so we will use the command input. As per documentation the input command in python returns a string.
  2. The second step is to take the input and print it in lowercase. For this we can use the method lower.

The solution will then be:

text = input()
print(str.lower(text))

GitHub repository

Playback Speed

This problems simulates a slown down speach. The objective here is to replace the space between words with ... (i.e., three periods).

Here are the steps to solve this problem:

  1. First we need to take user’s input, so we will use the command input. As per documentation the input command in python returns a string.
  2. The second step is to take the input and print it with ... in place of spaces. For this we can use the method replace.

The solution will then be:

text = input()
print(text.replace(' ', '...'))

GitHub repository

Making Faces

This problem brings that before emojis there where emoticons, which are text representations of the writer’s mood like the facial expressions :-D to show happiness.

The objective here is to replace two emoticons: :( per 🙁 and :) per 🙂 when user inputs these emoticons.

For this problem we will use the method replace. Since a emoji is just a representation of a caractere we can justa copy and paste it in our code.

The function main takes the input from user and print the emojis if they exist on the users input.

The function convert takes a str as parameter to replace the chracters with emoticons to emojis.

def main():
    text = input()
    print(convert(text))

def convert(text):
    text = text.replace(':)', '🙂')
    text = text.replace(':(', '🙁')

    return text

main()

GitHub repository

Einstein

For the problem Einstein is asked to print the value of energy (Joules) after user inputs a value representing the mass (kilograms), assuming that the user will input a interger.

\[E = m \times c ^ 2\]

Where c = 300,000,000 m/s

To solve this problem we will use the class int to change the users input from string to an integer number.

After changing the value input to a integer variable we can calculate the energy E.

m = int(input('m: '))
E = m * 300000000**2
print(f'E: {E}')

GitHub repository

Tip Calculator

The objective here is to complete the code given so we have a program that calculates the tip for the waiter that served you.

There are missing two functions:

  1. dollars_to_float: we will use the method removeprefix to remove the dollar symbol ($) and the function float to change the user’s input to a float number.
  2. percent_to_float: we will use the method removesufix to remove the percent symbol (\%) and the function float to change the user’s input to a float number.
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):
    d = d.removeprefix('$')
    return float(d)

def percent_to_float(p):
    p = p.removesuffix('%')
    return float(p) / 100

main()

GitHub repository

This is all for week zero - Functions, Variables, set of problems of CS50’s Introduction to Programming with Python course.