Problem Set 7: CS50 Finance

Building an app is a process that needs structure

I made an initial attempt that went very poorly. I read the CS50 assignment points and went through building the app page for page. Make all the code to register, then make all the code to get a quote, then buy, then sell. I started getting very picky about how the user got feedback when something was posted. I wanted nice banners to show things were successful or unsuccessful. I wanted javascript on the front end to update values. Everything became a big mess.

So i deleted it all. And pulled out some post-it notes, and a pencil.

I made 3 stacks:

  • Frontend
  • Database
  • Logic

Then I started writing down all the tasks as I read them on the CS50 assignment page. Each post was very simple and specific. On the top left corner of each post-it I put the first letter of what stack it would go on. On the top right I put the first letter for the webpage (register, buy, sell, quote etc).

A post-it might be “create view template register.html,” which would be a F on the top left.

another might be “create model for users,” which would be a D.

“create get_balance method for users,” another D.

What I found was the Frontend stack could be done very quickly because I noticed there were similar tasks. They weren’t directly connected the way a user thinks when reading about the app. They were literally the same task just for a different page of the app.

So my advice would be to write out as many tasks as possible, sort them in this way, and try to develop frontend -> database -> logic.

For the database I highly recommend creating a new file called and creating a class for each part of your app. I created users, transactions and portfolio. Here is what that looks like:

During the database phase I recommend using the python terminal REPL. If you go into the directory that holds for the flask app, you can actually test out any function, class or method right in the terminal. Here is an example of me using the terminal to test some of the user database methods:

type python -i This is interactive mode and makes testing models easier so you don’t have to submit forms.

When you finish the database make sure to add some fake data to save time during the logic portion.

Logic is something like “if the user does not have enough cash to by stocks return apology.”

API changes

In the helpers file on line 42 I changed the url because the one given doesn’t work. Here is the actual api now

change to this on line 42:

        response = requests.get(f"{urllib.parse.quote_plus(symbol)}/quote?token=YOUR_FREE_APITOKEN")

change your_free_api_token with your own token when you sign up.

Other quick notes


foreign keys:

inner joins:

Using the with keyword for opening and closing sqlite3

In python there is a way to open a sql connection and automatically close it using with.

Here would be the standard way of connecting to a database and closing it at the end.

import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()

c.execute('''CREATE TABLE stocks
             (date text, trans text, symbol text, qty real, price real)''')
c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")

# Save (commit) the changes

# close the connection if we are done with it.

Now using with:

import sqlite3
  with sqlite3.connect('example.db') as conn:
    c = conn.cursor()

    c.execute('''CREATE TABLE stocks
                 (date text, trans text, symbol text, qty real, price real)''')
    c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")

    # Save (commit) the changes

    # connection automatically closed once we leave this indented section of code


This was a much bigger project, so if you have any questions please let me know and i’ll do my best to answer. My code will be up on github:

Leave a Reply

1 Comment threads
1 Thread replies
Most reacted comment
Hottest comment thread
2 Comment authors
JohnnySalomon Dushimirimana Recent comment authors
newest oldest most voted
Notify of
Salomon Dushimirimana
Salomon Dushimirimana

Hi, thank you for the information. I also recently started working on this process. However, as I started to run my application, I got an error that I can’t solve: AttributeError: ‘users_db’ object has no attribute ‘register’

I don’t know if you will ever get this, but if you do, I really need your help

Thank you!