Bitcoin Price Index - CS50's Introduction to Programming with Python https://cs50.harvard.
edu/python/2022/psets/4/bitcoin/
CS50’s Introduction to Programming with Python
OpenCourseWare
Donate (https://cs50.harvard.edu/donate)
David J. Malan (https://cs.harvard.edu/malan/)
[email protected] (https://www.facebook.com/dmalan) (https://github.com/dmalan) (https://
www.instagram.com/davidjmalan/) (https://www.linkedin.com/in/malan/)
(https://www.reddit.com/user/davidjmalan) (https://www.threads.net/
@davidjmalan) (https://twitter.com/davidjmalan)
Bitcoin Price Index
As of Wednesday, February 12, 2025 at 5:00 PM GMT+4 (https://
time.cs50.io/20250212T130000Z), we are aware of some issues with the Coindesk API
that may affect students’ ability to complete this problem. We have now updated the
problem to use the CoinCap API instead. If you have already started working on this
problem, you may need to update your code to use the CoinCap API instead of the
Coindesk API.
Bitcoin (https://en.wikipedia.org/wiki/Bitcoin) is a form of digital
currency, otherwise known as cryptocurrency (https://en.wikipedia.org/
wiki/Cryptocurrency). Rather than rely on a central authority like a bank,
Bitcoin instead relies on a distributed network, otherwise known as a
blockchain (https://en.wikipedia.org/wiki/Blockchain), to record
transactions.
Because there’s demand for Bitcoin (i.e., users want it), users are willing
to buy it, as by exchanging one currency (e.g., USD) for Bitcoin.
In a �le called bitcoin.py , implement a program that:
Expects the user to specify as a command-line argument the number of Bitcoins, n, that
1 of 5 3/23/2025, 12:57 PM
Bitcoin Price Index - CS50's Introduction to Programming with Python https://cs50.harvard.edu/python/2022/psets/4/bitcoin/
they would like to buy. If that argument cannot be converted to a float , the program
should exit via sys.exit with an error message.
Queries the API for the CoinCap Bitcoin Price Index at https://api.coincap.io/v2/assets/
bitcoin (https://api.coincap.io/v2/assets/bitcoin), which returns a JSON (https://
en.wikipedia.org/wiki/JSON) object, among whose nested keys is the current price of
Bitcoin as a float . Be sure to catch any exceptions (https://requests.readthedocs.io/en/
latest/api/#exceptions), as with code like:
import requests
try:
...
except requests.RequestException:
...
Outputs the current cost of n Bitcoins in USD to four decimal places, using , as a
thousands separator.
▾ Hints
Recall that the sys module comes with argv , per docs.python.org/3/library/
sys.html#sys.argv (https://docs.python.org/3/library/sys.html#sys.argv).
Note that the requests module comes with quite a few methods, per
requests.readthedocs.io/en/latest (https://requests.readthedocs.io/en/latest/), among
which are get , per requests.readthedocs.io/en/latest/user/quickstart.html#make-a-
request (https://requests.readthedocs.io/en/latest/user/quickstart.html#make-a-request),
and json , per requests.readthedocs.io/en/latest/user/quickstart.html#json-response-
content (https://requests.readthedocs.io/en/latest/user/quickstart.html#json-response-
content). You can install it with:
pip install requests
Note that CoinCap’s API returns a JSON response like:
{
"data": {
"id": "bitcoin",
"rank": "1",
"symbol": "BTC",
"name": "Bitcoin",
"supply": "19823321.0000000000000000",
"maxSupply": "21000000.0000000000000000",
"marketCapUsd": "1939613325892.4607145113457500",
"volumeUsd24Hr": "12341417371.3505338276601668",
"priceUsd": "97845.0243474572557500",
"changePercent24Hr": "1.4324165997531723",
"vwap24Hr": "96203.8859537212418977",
2 of 5 3/23/2025, 12:57 PM
Bitcoin Price Index - CS50's Introduction to Programming with Python https://cs50.harvard.edu/python/2022/psets/4/bitcoin/
"explorer": "https://blockchain.info/"
},
"timestamp": 1739399343596
}
Recall that you can format USD to four decimal places with a thousands separator
(https://docs.python.org/3/library/string.html#formatspec) with code like:
print(f"${amount:,.4f}")
Demo
This demo was recorded when the price of Bitcoin was $38,761.0833. Your own output may
vary.
$ python bitcoin.py
Missing command-line argument
$ python bitcoin.py cat
Command-line argument is not a number
$ python bitcoin.py 1
$38,761.0833
$ python bitcoin.py 1.5
$58,141.6249
$ python bitcoin.py 2
$77,522.1666
$
Recorded with asciinema
Before You Begin
Log into cs50.dev (https://cs50.dev/), click on your terminal window, and execute cd by itself.
You should �nd that your terminal window’s prompt resembles the below:
3 of 5 3/23/2025, 12:57 PM