Answer to Question #238697 in Python for Joel

Question #238697



def countdown(n):

if n <= 0:

print('Blastoff!')

else:

print(n)

countdown(n-1)


Write a new recursive function countup that expects a negative argument and counts “up” from that number. Output from running the function should look something like this:


>>> countup(-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 countup. Choose for yourself which function to call (countdown or countup) for input of zero.


Provide the following.


The code of your program.

Output for the following input: a positive number, a negative number, and zero.

An explanation of your choice for what to call for input of zero.



1
Expert's answer
2021-09-17T17:42:02-0400
import sys
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)
 
if sys.version_info[0] == 5:
  number = int(input('Enter an interger: '))
else:
  number = int(input('Enter an interger: '))


if number> 0:
  countdown(number)
elif number < 0:
  countup(number)
else:
  countup(number)

output for a negative number

Enter an negative interger: -5

-5

-4

-3

-2

-1

Blastoff!

output for a positive number

Enter an positive interger: 5

5

4

3

2

1

Blastoff!

output for zero

Enter an interger: 0

Blastoff!


Zero is neither a positive nor a negative interger therefore the same output will be displayed if you call either of the function countup or countdown.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS