kathrinschuler / slide-python-intro-level2

Follow up course of "Introduction to Python", taking automation to the next level
https://kathrinschuler.github.io/slide-python-intro-level2
MIT License
1 stars 6 forks source link

Lesson 1 #7

Open nkeating5 opened 5 years ago

nkeating5 commented 5 years ago

Review of level 1 concepts via a solution to practise project which we send out before.

-Reading and writing files https://automatetheboringstuff.com/chapter8/

-General course set up - are we sticking with jupyter notebooks/repl combination

sarahmonod commented 5 years ago

Here is an example of a simple hangman implementation using lists, control statements, the random package, getting input from the command line. It doesn't have all of the concepts that were taught in level 1 (does not require dicts, does not need function definitions, and does not use excel), but I think it is better to make it simple enough to do quickly.

We could show the example runs below and then go over the code for an example of how to do it once they have tried.

$ python hangman
Number of wrongs: 0
_ _ _ _ _

Try a letter: a
Number of wrongs: 1
_ _ _ _ _

Try a letter: o
Number of wrongs: 1
_ _ _ _ o

Try a letter: w
Number of wrongs: 2
_ _ _ _ o

Try a letter: e
Number of wrongs: 2
_ e _ _ o

Try a letter: l
Number of wrongs: 2
_ e l l o

Try a letter: h
Congratulations! You found the word: hello
$ python hangman
Number of wrongs: 0
_ _ _ _ _

Try a letter: a
Number of wrongs: 1
_ _ _ _ _

Try a letter: o
Number of wrongs: 1
_ o _ _ _

Try a letter: w
Number of wrongs: 1
w o _ _ _

Try a letter: s
Number of wrongs: 2
w o _ _ _

Try a letter: p
Sorry, the word was: world
import random

MAX_WRONG = 3
WORDS = ['hello', 'world', 'whisky']

chosen = random.choice(WORDS)
found_letters = ['_'] * len(chosen)
nb_wrong = 0

while '_' in found_letters and nb_wrong < MAX_WRONG:
    print('Number of wrongs:', nb_wrong)
    print(' '.join(found_letters))
    print()
    attempt = input('Try a letter: ')[0]
    wrong = True
    for i, _ in enumerate(found_letters):
        if attempt == chosen[i]:
            wrong = False
            found_letters[i] = attempt
    if wrong:
        nb_wrong += 1

if nb_wrong < MAX_WRONG:
    print('Congratulations! You found the word:', chosen)
else:
    print('Sorry, the word was:', chosen)
micheleAlberto commented 5 years ago

@nkeating5 how tight do you think this should be with respect to the book? like should the presentation go through all the APIs explored by the book and possibly keep the same examples or are we aiming at squeezed down version?

How long is a lesson supposed to take? ~2h?

nkeating5 commented 5 years ago

It's a squeezed down version, and the book is supplemenatry so we can skip some of the finer details. A class take around 2hours. I think we should spend no more than 90 minutes on this. I'd say file paths are a must. It's fine to lift examples from this book.

We look at the pandas api to read a csv seperately. And we also covered the excel workbook reading/writing apis in level one.