diff --git a/.gitignore b/.gitignore index 733a58d..06a6c88 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.swp *.pdf *.docx .~lock* diff --git a/assets/text_based_adventure_game.dia b/assets/text_based_adventure_game.dia new file mode 100644 index 0000000..92c4e29 --- /dev/null +++ b/assets/text_based_adventure_game.dia @@ -0,0 +1,1003 @@ + + + + + + + + + + + + + #A4# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #You're in a cross section. +Do you go left or right?# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #Down this hall you encounter a bear. +It runs towards you. +Do you fight it?# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #A wizard appears who asks +you the meaning of life...# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #game over# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #You find some mushrooms. +Do you eat them?# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #You starve to dead...# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #The bear counter attacks! +You die...# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #That's right! He promotes you +to wizard level!# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #The wizard curses you to a life +of damnation# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #you win# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/assets/text_based_adventure_game.png b/assets/text_based_adventure_game.png new file mode 100644 index 0000000..15384db Binary files /dev/null and b/assets/text_based_adventure_game.png differ diff --git a/examples/string_format.py b/examples/string_format.py new file mode 100644 index 0000000..554a100 --- /dev/null +++ b/examples/string_format.py @@ -0,0 +1,18 @@ +# Basic placeholders + +ingredient = "sugar" +quantity = "500g" + +ingredient_string = "- {}: {}".format(ingredient, quantity) + +print(ingredient_string) + + +# Named placeholders + +fname = "John" +lname = "Doe" + +bond_string = "My name is {lastname}, {firstname} {lastname}".format(firstname=fname, lastname=lname) + +print(bond_string) diff --git a/learning_python3.md b/learning_python3.md index 18023ff..39ab1be 100644 --- a/learning_python3.md +++ b/learning_python3.md @@ -420,7 +420,60 @@ else: # A text based adventure game -TODO mini text based adventure game to point out the complexity of conditional logic and flow control +We can use conditional logic to create quite elaborate decision processes. +Let's build a mini text based adventure game. +Granted it's not a *tripple A* game but it will train your `if` and `else` skills plus it will highlight some issues we'll overcome in the next section. +Consider the diagram below. + +![adventure game](./assets/text_based_adventure_game.png) + +```python3 +answer = input("You're at a cross section. Do you go left or right?") +if answer.startswith("l"): + answer = input("Down this hall you encounter a bear. Do you fight it?") + if answer.startswith("y"): + print("The bear counter attack! He kills you") + print("game over!") + exit(0) + elif answer.startswith("n"): + print("It's a friendly bear! He transforms into a wizard!") + answer = input("The wizard asks you if you know the meaning of life?") + if answer == "42": + print("He knods approuvingly and upgrades you to wizard status!") + print("You win!") + exit(0) + else: + print("He shakes his head in disbelief. You fool!") + print("game over!") + else: + print("that's not a valid choice...") + print("game over!") + exit(0) +elif answer.startswith("r"): + answer = input("Down this hall you find some mushrooms. Do you eat them?") + if answer.startswith("n"): + print("You starve to dead...") + print("game over!") + exit(0) + elif answer.startswith("y"): + print("A wizard apprears out of thin air!") + answer = input("The wizard asks you if you know the meaning of life?") + if answer == "42": + print("He knods approuvingly and upgrades you to wizard status!") + print("You win!") + exit(0) + else: + print("He shakes his head in disbelief. You fool!") + print("game over!") + else: + print("that's not a valid choice...") + print("game over!") + exit(0) + pass +else: + print("game over!") + exit(0) +``` # Creating your own functions