Quiz 1 Review
CS 110
Reminders and Announcements
This Week
Quiz 1 - Wednesday (2/5)
Quiz 1 - Wednesday (2/5) Logistics
Variable Scope
5
Scope
6
Scope 1: Consider the following program...
def demo_1(name):
greeting = 'Welcome, ' + name
demo_1('Jimmy')
print(greeting)
7
Scope 1: Local variables cannot be accessed outside function
def demo_1(name):
greeting = 'Welcome, ' + name
demo_1('Jimmy')
print(greeting)
�TAKEAWAY: If you want access to a value after the function ends, you have to return it. WE CAN TURN THIS INTO A GOOD PROGRAMMING PRACTICE.
8
local variable
greeting variable was local to the demo_1 function. Throws error.
name = 'Lindsay'
def demo_2(name):
print(name)
demo_2('Walter')
TAKEAWAY: Local variables take precedence over global variables. THIS IS A BAD PROGRAMMING PRACTICE
Scope 2: Which name will print to the screen?
9
Global variable
Parameters are local variables. �They take precedence over global variables
Review Time
10
Reviewing and having a hard time understanding what's going on? Try pythontutor.com to help visualize what's going on!
Operators - What's in x,y,z?
x = 0
y = 6
z = 2
x = z ** z
y = (y % 2) + 5
z = x - (y - 4) // 3
Sequences - Value and Type
some_stuff = [(8, 9, 8), (1, "b", 3), ("a", 6), [0, 7]]
result_a = some_stuff[2][0]
result_b = some_stuff[some_stuff[1][0]][1]
thing = 1
result_c = some_stuff[2 + 2 - thing]
Calling Functions - Values of x,y,z, pineapple
def never(a, b):
return a % b
def gonna(a, b=1.0):
return b + a
def give():
return 5
def you(a=5):
return a
def up(pizza):
print("pizza")
x = gonna(5)
y = never(3, x)
z = gonna(x, b=give())
pineapple = up("hello")
Lists - What gets outputted? (run in Python!)
favorite_colors = ['Red', 'Orange', 'Yellow', 'Green', 'Blue']
favorite_colors.append('Indigo')
favorite_colors.append('Violet')
favorite_color = favorite_colors.pop(2)
favorite_colors.append(favorite_color)
favorite_color = favorite_colors.pop(0)
favorite_colors.append(favorite_color)
print(favorite_colors) # what will be printed out?
Calling Functions (run in Python!)
1. colors = ["red", "pink", "purple", "orange", "teal", "blue"]
2.
3. def car(coordinates, size, color="brown"):
4. print(coordinates, color, size)
5. print(coordinates, color, size)
6. print(coordinates, color, size)
7.
8. car((10, 10), 50, color="blue")
9. car((500, 500), 80, color=colors[5 % 3])
10. car((100, 130), 60)
11. car((100, 100), color="yellow", 80)
12. car((354, 487), 10, color=colors[len(colors)])
13. car((100, 100), "blue", color=10)
Writing Functions
Write a function random_quote that has the following inputs:
It should print a random quote from the list (Hint: You can assume you have already imported all the functions from the random module).
Example Function Call:
quotes = ["it's a me, mario", 'here we go!', 'mama mia!', 'wahoo!']
random_quote(quotes)
Example Output:
here we go!
Solution
def random_quote(quotes):
print(choice(quotes))
# Alternate Solution
def random_quote(quotes):
length = len(quotes)
print(randint(0, length -1))
Writing Functions
Write a reporter repeat_word that has the following inputs:
It should give back a string containing the specified word repeated n-times separated by the delimiter parameter.
Example Function Call:
result = repeat_word("banana", 10, delimiter='~')
print(result)
Example Output:
banana~banana~banana~banana~banana~banana~banana~banana~banana~banana
Example Solution
def repeat_word(word, n, delimiter=" "):
final_string = (word + delimiter) * (n - 1)
final_string = final_string + word
return final_string # NEEDS TO RETURN!