llnu entries
This commit is contained in:
parent
f5ddee1da4
commit
4889241581
5 changed files with 166 additions and 0 deletions
44
balazs/python-the-hard-way/ex32/notes_n_drills.org
Normal file
44
balazs/python-the-hard-way/ex32/notes_n_drills.org
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
* Drills
|
||||||
|
|
||||||
|
** Take a look at how you used range . Look up the range function to understand it.
|
||||||
|
|
||||||
|
tldr, a "function" that generates a sequence of numbers:
|
||||||
|
|
||||||
|
|
||||||
|
class range(start, stop[, step]):
|
||||||
|
start
|
||||||
|
|
||||||
|
The value of the start parameter (or 0 if the parameter was not supplied)
|
||||||
|
|
||||||
|
stop
|
||||||
|
|
||||||
|
The value of the stop parameter
|
||||||
|
|
||||||
|
step
|
||||||
|
|
||||||
|
The value of the step parameter (or 1 if the parameter was not supplied)
|
||||||
|
|
||||||
|
-------
|
||||||
|
|
||||||
|
I found it among python 3.8.3 built-in functions.
|
||||||
|
|
||||||
|
but!
|
||||||
|
|
||||||
|
Turns out it's not a function after all!
|
||||||
|
|
||||||
|
It's an immutable sequence type.
|
||||||
|
|
||||||
|
"a range object will always take the same (small) amount of memory" ... "as it only stores the start, stop and step values" and generates numbers on the fly
|
||||||
|
|
||||||
|
note: similar in shell is: seq
|
||||||
|
source: https://docs.python.org/3/library/stdtypes.html#range
|
||||||
|
|
||||||
|
** Could you have avoided that for-loop entirely on line 22 and just assigned range(0,6) directly to elements?
|
||||||
|
Do you mean line 21 book? (typo)
|
||||||
|
|
||||||
|
Yes! Because range will generate a list that can be appended added as is.
|
||||||
|
** Find the Python documentation on lists and read about them. What other operations can you do to lists besides append?
|
||||||
|
|
||||||
|
extend, insert, remove, pop, clear, index, count, sort, reverse, copy
|
||||||
|
|
||||||
|
fascinating! programming is cool! you can do so much with it
|
22
balazs/python-the-hard-way/ex33/ex33.py
Normal file
22
balazs/python-the-hard-way/ex33/ex33.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#i = 0
|
||||||
|
numbers = []
|
||||||
|
|
||||||
|
def counter(i2, INC_BY):
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
while i < i2 :
|
||||||
|
print(f"At the top i is {i}")
|
||||||
|
numbers.append(i)
|
||||||
|
|
||||||
|
i = i + INC_BY
|
||||||
|
|
||||||
|
print("Nums now:", numbers)
|
||||||
|
print(f"At the bottom i is {i}")
|
||||||
|
|
||||||
|
print("The numbers: ")
|
||||||
|
|
||||||
|
for num in numbers:
|
||||||
|
print(num)
|
||||||
|
|
||||||
|
counter(2, 2)
|
6
balazs/python-the-hard-way/ex34/notes_n_drills.org
Normal file
6
balazs/python-the-hard-way/ex34/notes_n_drills.org
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
* Drills
|
||||||
|
|
||||||
|
** With what you know of the difference between these types of numbers, can you explain why the year 2010 in ”January 1, 2010,” really is 2010 and not 2009? (Hint: you can’t pick years at random.)
|
||||||
|
|
||||||
|
What is this question? :/
|
||||||
|
|
84
balazs/python-the-hard-way/ex35/ex35.py
Normal file
84
balazs/python-the-hard-way/ex35/ex35.py
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
from sys import exit
|
||||||
|
import time
|
||||||
|
|
||||||
|
def gold_room():
|
||||||
|
print("This room is full of gold. How much do you take?")
|
||||||
|
|
||||||
|
choice = input("> ")
|
||||||
|
if "0" in choice or "1" in choice:
|
||||||
|
how_much = int(choice)
|
||||||
|
else:
|
||||||
|
dead("Man, learn to type a number.")
|
||||||
|
|
||||||
|
if how_much < 50:
|
||||||
|
print("Nice, you're not greedy, you win!")
|
||||||
|
exit(0)
|
||||||
|
else:
|
||||||
|
dead("You greedy bastard!")
|
||||||
|
|
||||||
|
|
||||||
|
def bear_room():
|
||||||
|
print("There is bear here.")
|
||||||
|
print("The bear has a bunch of honey.")
|
||||||
|
print("The fat bear is in front of another door.")
|
||||||
|
print("How are you going to move the bear?")
|
||||||
|
bear_moved = False
|
||||||
|
|
||||||
|
while True:
|
||||||
|
choice = input("> ")
|
||||||
|
|
||||||
|
if choice == "take honey":
|
||||||
|
dead("The bear looks at you then slaps your face off.")
|
||||||
|
elif choice == "taunt bear" and not bear_moved:
|
||||||
|
print("The bear has moved from the door.")
|
||||||
|
print("You can go through it now.")
|
||||||
|
bear_moved = True
|
||||||
|
elif choice == "tea" and not bear_moved:
|
||||||
|
print("You have invited the bear for a tea.")
|
||||||
|
time.sleep(1)
|
||||||
|
print("The bear is happy and moved.")
|
||||||
|
bear_moved = True
|
||||||
|
elif choice == "taunt bear" and bear_moved:
|
||||||
|
dead("The bear gets pissed off and chews your leg off.")
|
||||||
|
elif choice == "open door" and bear_moved:
|
||||||
|
gold_room()
|
||||||
|
else:
|
||||||
|
print("I got no idea what that means.")
|
||||||
|
|
||||||
|
|
||||||
|
def cthulhu_room():
|
||||||
|
print("Here you see the great evil Cthulhu.")
|
||||||
|
print("He, it, whatever stares at you and you go insane.")
|
||||||
|
print("Do you flee for your life or eat your head?")
|
||||||
|
|
||||||
|
choice = input("> ")
|
||||||
|
|
||||||
|
if "flee" in choice:
|
||||||
|
start()
|
||||||
|
elif "head" in choice:
|
||||||
|
dead("Well that was tasty!")
|
||||||
|
else:
|
||||||
|
cthulhu_room()
|
||||||
|
|
||||||
|
|
||||||
|
def dead(why):
|
||||||
|
print(why, "Good job!")
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
|
||||||
|
def start():
|
||||||
|
print("You are in a dark room.")
|
||||||
|
print("There is a door to your right and left.")
|
||||||
|
print("Which one do you take?")
|
||||||
|
|
||||||
|
choice = input("> ")
|
||||||
|
|
||||||
|
if choice == "left":
|
||||||
|
bear_room()
|
||||||
|
elif choice == "right":
|
||||||
|
cthulhu_room()
|
||||||
|
else:
|
||||||
|
dead("You stumble around the room until you starve.")
|
||||||
|
|
||||||
|
|
||||||
|
start()
|
10
balazs/python-the-hard-way/ex35/notes_n_drills.org
Normal file
10
balazs/python-the-hard-way/ex35/notes_n_drills.org
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
*Drills
|
||||||
|
|
||||||
|
** The gold_room has a weird way of getting you to type a number. What are all the bugs in this way of doing it? Can you make it better than what I’ve written? Look at how int() works for clues.
|
||||||
|
|
||||||
|
Non greedy is less than 50 but only 1 and 0 qualify.
|
||||||
|
|
||||||
|
Nicer ways:
|
||||||
|
-> check for numbers
|
||||||
|
-> also check for None and All strings
|
||||||
|
-> sanitize input for int(), (int can take 2 variables)
|
Loading…
Reference in a new issue