1. Copy the countdown function from Section 5.8 of your textbook.
def countdown(n):
if n <= 0:
print('Blastoff!')
else:
print(n)
countdown(n-1)
Write a new recursive function count up that expects a negative argument and counts “up” from that number. Output from running the function should look something like this:
>>> count up(-3)
-3
-2
-1
Blastoff!
Write a Python program that gets a number using keyboard input. (Remember to use input for Python 3 but raw_input for Python 2.)
If the number is positive, the program should call countdown. If the number is negative, the program should call count up. Choose for yourself which function to call (countdown or count up) for input of zero.
Provide the following.
def countdown(n):
if n <= 0:
print('Blastoff!')
else:
print(n)
countdown(n-1)
def countup(n):
if n >= 0:
print('Blastoff!')
else:
print(n)
countup(n+1)
n = int(input())
if n < 0:
countup(n)
else:
countdown(n)
for zero, use the countdown() function, because zero has no minus sign and appears to be more positive than negative.
Comments
Leave a comment