Computers and Technology
Computers and Technology, 14.02.2021 22:00, maddy6882

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

The below is the textbook.

5.8 Recursion
It is legal for one function to call another; it is also legal for a function to call itself. It may
not be obvious why that is a good thing, but it turns out to be one of the most magical
things a program can do. For example, look at the following function:
def countdown(n):
if n <= 0:
print('Blastoff!')
else:
print(n)
countdown(n-1)
If n is 0 or negative, it outputs the word, “Blastoff!” Otherwise, it outputs n and then calls
a function named countdown—itself—passing n-1 as an argument.
What happens if we call this function like this?
>>> countdown(3)
The execution of countdown begins with n=3, and since n is greater than 0, it outputs the
value 3, and then calls itself...
The execution of countdown begins with n=2, and since n is greater than 0, it
outputs the value 2, and then calls itself...
The execution of countdown begins with n=1, and since n is greater
than 0, it outputs the value 1, and then calls itself...
The execution of countdown begins with n=0, and since n is
not greater than 0, it outputs the word, “Blastoff!” and then
returns.
The countdown that got n=1 returns.
The countdown that got n=2 returns.
The countdown that got n=3 returns.
And then you’re back in __main__. So, the total output looks like this:
3
2
1
Blastoff!
A function that calls itself is recursive; the process of executing it is called recursion.
As another example, we can write a function that prints a string n times.
def print_n(s, n):
if n <= 0:
return
print(s)
print_n(s, n-1)

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 06:40, euniceyi56
How many nibbles can be stored in a 16-bit word?
Answers: 1
image
Computers and Technology, 23.06.2019 22:30, cuki96
Lakendra finished working on her monthly report. in looking it over, she saw that it had large blocks of white space. what steps could lakendra take to reduce the amount of white space?
Answers: 3
image
Computers and Technology, 24.06.2019 00:40, dheydar3506
What is the error in the following pseudocode? module main() call raisetopower(2, 1.5) end module module raisetopower(real value, integer power) declare real result set result = value^power display result end module
Answers: 1
image
Computers and Technology, 24.06.2019 15:30, jhony70
What type of forensic evidence was recovered during the bomb set off at the new mexico facility on the video that was similar to the evidence obtained at the boston bombings and how did the evidence allow the researchers to connect other pieces of evidence to the same bomb?
Answers: 2
Do you know the correct answer?
1. Copy the countdown function from Section 5.8 of your textbook. def countdown(n):
if n <...

Questions in other subjects:

Konu
Mathematics, 10.09.2020 21:01
Konu
Mathematics, 10.09.2020 21:01
Konu
History, 10.09.2020 21:01
Konu
Mathematics, 10.09.2020 21:01
Konu
Mathematics, 10.09.2020 21:01
Konu
Mathematics, 10.09.2020 21:01
Konu
Mathematics, 10.09.2020 21:01
Konu
Mathematics, 10.09.2020 21:01
Konu
Mathematics, 10.09.2020 21:01
Konu
Mathematics, 10.09.2020 21:01