I need to write a function that prints a timer as output (everything at the same time). However, I need to write "Ready?" at the beginning, so I only want it to be printed once.
Here's what I have so far:
def factorial(i):
while i > 0:
print(i)
return (factorial(i-1))
print('Go!')
factorial(5)
I want the function to print the output like so:
Ready?
5
4
3
2
1
Go!
You can use an inner function :
def factorial(i):
def recursive_output(i):
if i > 0:
print(i)
recursive_output(i-1)
print("Ready!")
recursive_output(i)
print("Go!")
You can add an argument to the function
FIRST_TIME = True
NOT_FIRST_TIME = False
def factorial(i, is_first_time):
if is_first_time:
print('Ready?')
if i:
print(i)
return (factorial(i-1, NOT_FIRST_TIME))
print('Go!')
factorial(5, FIRST_TIME)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.