Week 4
Libraries
A library is functions someone else already wrote. You import them, or install them first and then import them.
Import loads a library. From imports one name out of it, so you can call that name without the library's prefix. Random can pick from a list, shuffle a list, or give an integer between two ends, and both ends count. Statistics can average a list. Those two ship with Python.
Sys.argv is the list of words typed after python. Item 0 is the program's own file name. If you read an item that is not there, you get an IndexError, so check the length first or stop with sys.exit. A slice such as argv[1:] drops the file name and keeps the rest.
Packages that are not built in are installed with pip, then imported the same way. Requests gets text from the web. That text is often JSON, which Python turns into nested dictionaries. You open them by key, outer box first. A fresh example is the cafe menu toy on this page, not the bitcoin problem.
Try this first
A die. Not a problem set.
Both ends can come up. Roll until you have seen 1 and 6.
A cafe menu. Not a problem set.
Open the outer box, then the inner one. The price is inside.
data
4.50
Problems
A gentle order is Emojize, Guessing Game, Little Professor, Adieu, Figlet, then Bitcoin. The course does not require that order. You still submit all six.
- EmojizeInstall a package, import it, and call the one function the spec names.
- Guessing GameA random integer, and two conversations that ask again when the input is bad.
- Little ProfessorThe same random integer, split into the functions the spec already names.
- Adieu, AdieuA list you grow until the person ends input, then a package that knows English commas.
- Frank, Ian and Glen's LettersThe command line, a package that draws letters, and a random choice when the person does not pick a font.
- Bitcoin Price IndexOne number from the command line, then a dictionary that arrived from the web.