Learning Python Functions and Variables with Harvard CS50: My First Coding Experience

This is my first ever blog on this site and my first experience with coding. Knowing nothing and being very broke, I decided to teach myself to code using the wonders of the internet; specifically Harvard’s CS50 course. CS50 is a free introduction to Python course taught by Harvard professor David Milan.

The course begins with Week 0: Functions and Variables. I started by watching the two-hour introduction video and reviewing the notes. I learned about all sorts of different functions, such as print, variables, define, strip, capitalize, integers, floats, math functions, etc. For example, here are a couple of snippets from the lecture.

Here is the calculator.py from the lecture:

x = float(input("What's x? "))
y = float(input("what's y? "))

z= round(x/y, 2)

print(f"{z:,}")

However, you can only learn so much by watching videos, which is why CS50 has problem sets for each week. Problem sets are a variety of complex and intuitive challenges the student is meant to complete by using the lessons taught in that week. Here I tackled problem set 0. If you want to complete CS50 these are the answers for the problem sets.

Problem 1: Indoor Voices

This problem set is super simple; you must convert any input text into all lowercase. However, I got frustrated because while it was so simple, I couldn’t figure out how. Then I realized we’re supposed to find functions in the Python documentation that weren’t covered in the original lesson. After doing this and googling a little bit, I found the casefold function, which solved the problem set.

print(input().casefold())
Problem 2: Playback Speed

This problem set involved taking any input text and putting a “…” in between every word. I thought this would be a simple print input function that I could change the sep attribute. However, I got very frustrated because I hadn’t yet realized that the sep attribute only separated the *objects in the print function. Since I was inputting the input string text as one singular object, the sep attribute was changing what was inside the object. Then I found a helpful article by Medium user l1gend and discovered the replace function.

userinput = input()

print( userinput.replace(" ", "..."))
Problem 3: Making Faces

This problem set basically involves replacing emoticons [ex. 🙂 or 🙁 ] with emojis. Essentially I just used the exact same concept as problem 2, the replace function, to swap the emoticons with their respective emojis. The only thing complicated is using the emojis in the code, but the emojis really just function exactly as string functions so it was pretty easy.

userinput = input()

print(userinput.replace(":)", "🙂").replace(":(", "🙁"))
Problem 4: Einstein

In this problem, we are solving Albert Einstein’s famous equation E=mc^2. Given that c (the speed of light) is 300 million meters per second and asking for variable m (mass in kilograms), output the energy in joules. We are setting up a simple math problem where we find E for any integer value of m.

m = int(input("m: "))
c = 300000000**2
print(m*c)
Problem 5: Tip Calculator

This problem was by far the hardest of the bunch. It starts by giving you this code for a tip calculator.

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()

The prompt is that you have to define the functions dollars_to_float and percent_to_float. I knew we had to remove the $ and % signs and this was easy using the replace function. I also knew that the inputs had to remain decimals so they had to be floats. The struggle I had was forgetting to divide the p value by 100 to convert it from percent and forgetting to return the values after defining the function.

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
    d = d.replace("$", "")
    d=float(d)
    return d


def percent_to_float(p):
    # TODO
    p = p.replace("%", "")
    p = float(p)/100
    return p
main()

This week wasn’t too hard and I am really looking forward to getting into the more complex stuff soon.