reviewing ex.15 to 26
This commit is contained in:
parent
402d63519d
commit
d270952bf0
6 changed files with 59 additions and 22 deletions
3
sami/learn-python-the-hard-way/ex15_sample.txt
Normal file
3
sami/learn-python-the-hard-way/ex15_sample.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
This is stuff I typed into a file.
|
||||||
|
It is really cool stuff.
|
||||||
|
Lots and lots of fun to have in here.
|
|
@ -1,22 +1,20 @@
|
||||||
# this one is like your scripts with argv
|
# this one is like your scripts with argv
|
||||||
def print_two(*args):
|
def print_two(*args):
|
||||||
arg1, arg2 = args
|
arg1, arg2 = args
|
||||||
print(f"arg1: {arg1}, arg2: {arg2}")
|
print(f"arg1: {arg1}, arg2: {arg2}")
|
||||||
|
|
||||||
# ok, that *args is actually pointless, we can just do this
|
# ok, that *args is actually pointless, we can just do this
|
||||||
def print_two_again(arg1, arg2):
|
def print_two_again(arg1, arg2):
|
||||||
|
print(f"arg1: {arg1}, arg2: {arg2}")
|
||||||
print(f"arg1: {arg1}, arg2: {arg2}")
|
|
||||||
|
|
||||||
# this just takes one argument
|
# this just takes one argument
|
||||||
def print_one(arg1):
|
def print_one(arg1):
|
||||||
|
print(f"arg1: {arg1}")
|
||||||
print(f"arg1: {arg1}")
|
|
||||||
|
|
||||||
# this one takes no arguments
|
# this one takes no arguments
|
||||||
def print_none():
|
def print_none():
|
||||||
|
print("I got nothin'.")
|
||||||
|
|
||||||
print("I got nothin'.")
|
|
||||||
|
|
||||||
|
|
||||||
print_two("Zed","Shaw")
|
print_two("Zed","Shaw")
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
def cheese_and_crackers(cheese_count, boxes_of_crackers):
|
def cheese_and_crackers(cheese_count, boxes_of_crackers):
|
||||||
print(f"You have {cheese_count} cheeses!")
|
print(f"You have {cheese_count} cheeses!")
|
||||||
print(f"You have {boxes_of_crackers} boxes of crackers!")
|
print(f"You have {boxes_of_crackers} boxes of crackers!")
|
||||||
print("Man that's enough for a party!")
|
print("Man that's enough for a party!")
|
||||||
print("Get a blanket.\n")
|
print("Get a blanket.\n")
|
||||||
|
|
||||||
|
|
||||||
print("We can just give the function numbers directly:")
|
print("We can just give the function numbers directly:")
|
||||||
|
|
|
@ -6,17 +6,17 @@ script, input_file = argv
|
||||||
|
|
||||||
|
|
||||||
def print_all(f):
|
def print_all(f):
|
||||||
print(f.read())
|
print(f.read())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def rewind(f):
|
def rewind(f):
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def print_a_line(line_count, f):
|
def print_a_line(line_count, f):
|
||||||
print(line_count, f.readline())
|
print(line_count, f.readline())
|
||||||
|
|
||||||
|
|
||||||
current_file = open(input_file)
|
current_file = open(input_file)
|
||||||
|
|
|
@ -1,21 +1,21 @@
|
||||||
def add(a, b):
|
def add(a, b):
|
||||||
print(f"ADDING {a} + {b}")
|
print(f"ADDING {a} + {b}")
|
||||||
return a + b
|
return a + b
|
||||||
|
|
||||||
|
|
||||||
def subtract(a, b):
|
def subtract(a, b):
|
||||||
print(f"SUBTRACTING {a} - {b}")
|
print(f"SUBTRACTING {a} - {b}")
|
||||||
return a - b
|
return a - b
|
||||||
|
|
||||||
|
|
||||||
def multiply(a, b):
|
def multiply(a, b):
|
||||||
print(f"MULTIPLYING {a} * {b}")
|
print(f"MULTIPLYING {a} * {b}")
|
||||||
return a * b
|
return a * b
|
||||||
|
|
||||||
|
|
||||||
def divide(a, b):
|
def divide(a, b):
|
||||||
print(f"DIVIDING {a} / {b}")
|
print(f"DIVIDING {a} / {b}")
|
||||||
return a / b
|
return a / b
|
||||||
|
|
||||||
|
|
||||||
print("Let's do some math with just functions!")
|
print("Let's do some math with just functions!")
|
||||||
|
|
36
sami/learn-python-the-hard-way/ex25.py
Normal file
36
sami/learn-python-the-hard-way/ex25.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
|
||||||
|
def break_words(stuff):
|
||||||
|
"""This function will break up words for us."""
|
||||||
|
words = stuff.split(' ')
|
||||||
|
return words
|
||||||
|
|
||||||
|
|
||||||
|
def sort_words(words):
|
||||||
|
"""Sorts the words."""
|
||||||
|
return sorted(words)
|
||||||
|
|
||||||
|
|
||||||
|
def print_first_word(words):
|
||||||
|
"""Prints the first word after popping it off."""
|
||||||
|
word = words.pop(0)
|
||||||
|
print(word)
|
||||||
|
|
||||||
|
|
||||||
|
def print_last_word(words):
|
||||||
|
"""Prints the last word after popping it off."""
|
||||||
|
word = words.pop(-1)
|
||||||
|
print(word)
|
||||||
|
|
||||||
|
|
||||||
|
def sort_sentence(sentence):
|
||||||
|
"""Takes in a full sentence and returns the sorted words."""
|
||||||
|
words = break_words(sentence)
|
||||||
|
return sort_words(words)
|
||||||
|
|
||||||
|
|
||||||
|
def print_first_and_last(sentence):
|
||||||
|
"""Prints the first and last words of the sentence."""
|
||||||
|
words = break_words(sentence)
|
||||||
|
print_first_word(words)
|
||||||
|
print_last_word(words)
|
||||||
|
|
Loading…
Reference in a new issue