reviewing ex.15 to 26

This commit is contained in:
samialazar 2020-05-26 11:52:28 +02:00
parent 402d63519d
commit d270952bf0
6 changed files with 59 additions and 22 deletions

View 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.

View file

@ -1,22 +1,20 @@
# this one is like your scripts with argv
def print_two(*args):
arg1, arg2 = args
print(f"arg1: {arg1}, arg2: {arg2}")
arg1, arg2 = args
print(f"arg1: {arg1}, arg2: {arg2}")
# ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2):
print(f"arg1: {arg1}, arg2: {arg2}")
print(f"arg1: {arg1}, arg2: {arg2}")
# this just takes one argument
def print_one(arg1):
print(f"arg1: {arg1}")
print(f"arg1: {arg1}")
# this one takes no arguments
def print_none():
print("I got nothin'.")
print("I got nothin'.")
print_two("Zed","Shaw")

View file

@ -1,8 +1,8 @@
def cheese_and_crackers(cheese_count, boxes_of_crackers):
print(f"You have {cheese_count} cheeses!")
print(f"You have {boxes_of_crackers} boxes of crackers!")
print("Man that's enough for a party!")
print("Get a blanket.\n")
print(f"You have {cheese_count} cheeses!")
print(f"You have {boxes_of_crackers} boxes of crackers!")
print("Man that's enough for a party!")
print("Get a blanket.\n")
print("We can just give the function numbers directly:")

View file

@ -6,17 +6,17 @@ script, input_file = argv
def print_all(f):
print(f.read())
print(f.read())
def rewind(f):
f.seek(0)
f.seek(0)
def print_a_line(line_count, f):
print(line_count, f.readline())
print(line_count, f.readline())
current_file = open(input_file)

View file

@ -1,21 +1,21 @@
def add(a, b):
print(f"ADDING {a} + {b}")
return a + b
print(f"ADDING {a} + {b}")
return a + b
def subtract(a, b):
print(f"SUBTRACTING {a} - {b}")
return a - b
print(f"SUBTRACTING {a} - {b}")
return a - b
def multiply(a, b):
print(f"MULTIPLYING {a} * {b}")
return a * b
print(f"MULTIPLYING {a} * {b}")
return a * b
def divide(a, b):
print(f"DIVIDING {a} / {b}")
return a / b
print(f"DIVIDING {a} / {b}")
return a / b
print("Let's do some math with just functions!")

View 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)