Learning Journal 3: Recursive functions countup/countdown PDF

Title Learning Journal 3: Recursive functions countup/countdown
Author Leonard Njue Kabiu
Course Programming Fundamentals
Institution University of the People
Pages 4
File Size 85.7 KB
File Type PDF
Total Downloads 105
Total Views 127

Summary

Write a new recursive function countup that expects a negative argument and counts “up” from that number.
A chained conditional is a conditional statement with a series of alternative branches while a nested conditional appears in one of the branches of another conditional statement...


Description

PART 1 Question 1. Write a new recursive function countup that expects a negative argument and counts “up” from that number. def countup(n): if n > 0: print('Blastoff!') else: print(n) countup(n+1)

The output (on python shell) of the above is; >>>countup(-8) -8 -7 -6 -5 -4 -3 -2 -1 0 Blastoff! >>>

Question 2. 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.

# countdown countup n = int(input('Enter value : ')) def countdown(n): if n 0: print('Blastoff!') else: print(n) countup(n+1) countup(n)

Output for a negative Number: Enter value : -4 -4 -3 -2 -1 0 Blastoff! >>>

Output for a Positive number: Enter value : 5 5 4 3 2 1 Blastoff! >>>

Output for zero:

Enter value : 0 0 Blastoff! >>>

I have chosen the output for zero as ‘blastoff!’ because zero is always the last number counted in a countdown. PART 2. Question Write your own unique Python program that has a runtime error. Do not copy the program from your textbook or the Internet. Provide the following.    

The code of your program. Output demonstrating the runtime error, including the error message. An explanation of the error message. An explanation of how to fix the error.

Answer # countdown countup n = input('Enter value : ') def countdown(n): if n 0: print('Blastoff!') else: print(n) countup(n+1) countup(n)

The output is as follows: Enter value : 9 Traceback (most recent call last): File "E:\Flash Backup\University of the People\Njue\Computer Science\Term 4\Assignments\countup.py", line 13, in

countdown(n) File "E:\Flash Backup\University of the People\Njue\Computer Science\Term 4\Assignments\countup.py", line 7, in countdown if n >>

One gets a type error, which in this case means that a string cannot be equated to an integer. I defined the letter string ‘enter value’ as an integer and the error was fixed. from n = input('Enter value : ')

to n = int(input('Enter value : '))

Reference: Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea Press. This book is licensed under Creative Commons Attribution-NonCommercial 3.0 Unported (CC BYNC 3.0)....


Similar Free PDFs