Answer to Question #332053 in Python for john

Question #332053

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
2022-04-21T14:37:02-0400


//1) When the number is less than zero, we call the countup function; 
//if it is greater than zero, then cuntdown
//3) To enter zero, you don’t need to call anything, you just need to output 0
#CounDown
def countdown(n):
   if n <= 0:
     print('Blastoff!')
   else:
     print(n)
     countdown(n-1)
#CoundUp
def counup(n):
  if(n>=0):
    print("Blastoff!")
  else:
    print(n)
    counup(n+1)
n=int(input('Input number:'))
if(n<0):#if n to be negative then call countuo
  counup(n)
elif(n>0):
  countdown(n)
else:
  print('0\nBlastoff!')

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