Recursive functions use this to stop recursive calls
What is the base case?
The keyword reserved for strings
What is str?
While loops take a _______ value to decide whether or not they repeat
What is a boolean value
The keyword used to define a function
What is def
Why can you not access variables declared inside a function outside of it?
What is scope
The method to track recursive function calls
What is substitution trace
what is s[-1:]
The method to track a variable between iterations
What is timeline trace
The name of the variables declared in the function header
What is parameter
Define declaring a variable
The process of telling the program that a specific variable exists and what it's name is.
Define tail recursion
What is a specific type of recursion where the recursive call is the absolute last operation performed by the function before it returns
How can you reverse a string with splicing?
what is [::-1]
This keyword ends the loop completely, even before its conditions are fulfilled.
What is break
Functions return this when there is no return statement
What is None?
The purpose of a main guard
its purpose is to control whether code runs automatically or only when the file is executed directly.
This happens when you call a recursive function too many times within itself.
What is a RecursionError
How does string splicing work?
Slicing lets you pull out a substring using the pattern: s[start:stop:step]
Rewrite this while loop as a for loop:
j = 0
while j < 77:
print("I'm currently", j, "years old")
j += 1
for j in range(77):
print("I'm currently", j, "years old")
This keyword is used inside a function to send a value back to the code that called it, immediately ending the function's execution at that point.
What is return
Free points! First to tell me their favorite board game wins!
Ark nova
The substitution trace of this function:
def board(game):
if (game == 0):
return "DONE"
if (game % 2 == 0):
return "J" + board(game - 1)
if (game > 5):
return "A" + board(game - 1)
if (game % 2 == 1):
return "L" + board(game - 1)
board(7)
board(0) = "DONE"
board(1) = "L" + board(0) = "L" + "DONE" = "LDONE"
board(2) = "J" + board(1) = "J" + "LDONE" = "JLDONE"
board(3) = "L" + board(2) = "L" + "JLDONE" = "LJLDONE"
board(4) = "J" + board(3) = "J" + "LJLDONE" = "JLJLDONE"
board(5) = "L" + board(4) = "L" + "JLJLDONE" = "LJLJLDONE"
board(6) = "J" + board(5) = "J" + "LJLJLDONE" = "JLJLJLDONE"
board(7) = "A" + board(6) = "A" + "JLJLJLDONE" = "AJLJLJLDONE"
What would be printed out by this code:
board = "game"
gamer = "board"
print(gamer[::-1] + board[0:5] + gamer[1:2])
what is draobgameo
Rewrite this recursive function iteratively
def sum_up_to(n, total=0):
if n == 0:
return total
return sum_up_to(n - 1, total + n)
print(sum_up_to(5))
def sum_up_to_loop(n):
total = 0
while n != 0:
total = total + n
n = n - 1
return total
print(sum_up_to_loop(5))
Write code of a function named "ark" with parameters "no" and "va" which multiplies both together and then adds the original values. Also call the function with the arguments "5" and "7"
def ark(no, va):
return (no * va) + no + va
ark(5, 7)
Write some code that opens file "boardgame.txt" and automatically closes it
with open( "boardgame.txt" ) as fd :
# doesn't matter