Answer to Question #112855 in Python for Joshua Oluwabusayo Obong

Question #112855
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 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
2020-04-30T13:37:43-0400
import sys

# count down function
def countdown(n):
  if n <= 0:
    print('Blastoff!')
  else:
    print(n)
    countdown(n-1)

# count up function
def countup(n):
  if n >= 0:
    print('Blastoff!')
  else:
    print(n)
    countup(n+1)

# ask user to enter number 
if sys.version_info[0] == 3:
  num = input('Enter number: ')
else:
  num = raw_input('Enter number: ')

# convert string to number
num = int(num)

if num > 0:
  # count down positive number
  countdown(num)
elif num < 0:
  # count up negative number
  countup(num)
else:
  # there is no difference which function to call
  # in case of 0, both functions will print 'Blastoff!'
  print('Blastoff!')

# Outputs:
# num=3    3 2 1 Blastoff!
# num=-3   -3 -2 -1 Blastoff!
# num=0    Blastoff!

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

Randon
07.12.23, 12:27

This website is very helpful. I can learn about coding and how to create a full program. It is a very good website.

ACHO BERNARD
03.12.21, 03:00

I love this site and will like to remain on it all the time just for exploits for better understanding.

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS