adds import
This commit is contained in:
parent
531f790c2f
commit
81625688a6
|
@ -716,9 +716,59 @@ pretty_print("Alice", "-")
|
|||
|
||||
# Using the standard library
|
||||
|
||||
TODO import random exercise (digital dice)
|
||||
TODO import datetime exercise (will it be Sunday)
|
||||
TODO simple ROT13 cryptography with multiple libs
|
||||
There is no need to reinvent the wheel each time you build a bike.
|
||||
The same goes for programming.
|
||||
Most, if not all, programming languages come with a **standard library** which is a collection of additional objects and functions to facilitate common problems.
|
||||
We'll look at some essential ones together but I urge you to [read](https://docs.python.org/3/library/index.html) up a bit when you have some free time.
|
||||
Over the course of you programming journey you'll discover that efficient programming is often finding the right libraries and chaining them together to suit your use case.
|
||||
|
||||
Imagine we want to include a dice in our text based adventure game.
|
||||
How on earth do we program that?
|
||||
We need some for of *randomness* in our code.
|
||||
A [quick google](https://stackoverflow.com/questions/28705965/python-random-function-without-using-random-module) demonstrates this is quite difficult without the use of libraries.
|
||||
As randomness is both [extensively](https://www.random.org/randomness/) used in programming **and** it's quite [difficult](https://medium.com/swlh/random-functions-a4f36b1dfd8f) to do properly, you'll find a `random` library in most languages.
|
||||
|
||||
The new **keyword** you'll learn here is `import`.
|
||||
It allows you to add extra functionality to your program.
|
||||
Once *imported* we can **call** functions that exist from the library in question.
|
||||
So, our dice becomes as follows.
|
||||
|
||||
```python3
|
||||
import random
|
||||
|
||||
throw = random.randint(1, 6)
|
||||
|
||||
print(throw)
|
||||
```
|
||||
|
||||
⛑ **Autocomplete is your friend. You can use it to browse around a library and discover interesting functions or classes you can make use of.**
|
||||
|
||||
A second widely used library is `datetime`.
|
||||
It facilitates handling dates, hours, calendars, time differences, etc.
|
||||
A simple program we can write to illustrate it's purpose is a *will-it-be-Sunday* program.
|
||||
You give date and the program tells you if it's a Sunday or not.
|
||||
|
||||
```python3
|
||||
import datetime
|
||||
|
||||
sunday = 6
|
||||
|
||||
date = input("which date should I look up? (example: 2022 3 23) ")
|
||||
year, month, day = date.split()
|
||||
|
||||
date = datetime.date(int(year), int(month), int(day))
|
||||
|
||||
if date.weekday() == sunday:
|
||||
print("yes! you can sleep in...")
|
||||
else:
|
||||
print("better set your alarm")
|
||||
```
|
||||
|
||||
The program above incorporates a lot of different concepts.
|
||||
Read it very slowly and think about what each step is doing.
|
||||
Also think about how you can *break* this program!
|
||||
|
||||
⛑ **Why on earth is Sunday six? Read the [doc](https://docs.python.org/3/library/datetime.html#datetime.date.weekday)!**
|
||||
|
||||
# Coding challenge - Memento Mori calculator
|
||||
|
||||
|
@ -751,6 +801,7 @@ TODO guess the number exercise
|
|||
# For loop
|
||||
|
||||
TODO say hello to my friends exercise
|
||||
TODO simple ROT13 cryptography with multiple libs
|
||||
|
||||
# Coding challenge - Cheerleader chant
|
||||
|
||||
|
|
Loading…
Reference in New Issue