In this code, there's an initialization problem that's causing our function to behave incorrectly. Can you find the problem and fix it?
Question:
def count_down(start_number):
while (current > 0):
print(current)
current -= 1
print("Zero!")
count_down(3)
Answers:
Running the code reveals exactly where the problem is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in count_down
UnboundLocalError: local variable 'current' referenced before assignment
Instead of current use start_number
def count_down(start_number):
while (start_number > 0):
print(start_number)
start_number -= 1
print("Zero!")
count_down(3)
output:
3
2
1
Zero!
def count_down(start_number):
while (current > 0):
print(current)
current -= 1
print("Zero!")
count_down(3)
Running the code reveals exactly where the problem is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in count_down
UnboundLocalError: local variable 'current' referenced before assignment
Instead of current use start_number
def count_down(start_number):
while (start_number > 0):
print(start_number)
start_number -= 1
print("Zero!")
count_down(3)
output:
3
2
1
Zero!