3.5.1: Recursive function: Writing the base case.   Add an if branch to complete double_pennies()'s base case. Sample output with inputs: 1 10 Number of pennies after 10 days: 1024 # Returns number of pennies if pennies are doubled num_days times def double_pennies(num_pennies, num_days):     total_pennies = 0     if "solution goes here"     else:         total_pennies = double_pennies((num_pennies * 2), (num_days - 1))     return total_pennies # Program computes pennies if you have 1 penny today, # 2 pennies after one day, 4 after two days, and so on starting_pennies = int(input()) user_days = int(input()) print('Number of pennies after', user_days, 'days: ', end="") print(double_pennies(starting_pennies, user_days))

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
3.5.1: Recursive function: Writing the base case.
 
Add an if branch to complete double_pennies()'s base case.

Sample output with inputs: 1 10
Number of pennies after 10 days: 1024

# Returns number of pennies if pennies are doubled num_days times
def double_pennies(num_pennies, num_days):
    total_pennies = 0

    if "solution goes here"

    else:
        total_pennies = double_pennies((num_pennies * 2), (num_days - 1))

    return total_pennies

# Program computes pennies if you have 1 penny today,
# 2 pennies after one day, 4 after two days, and so on
starting_pennies = int(input())
user_days = int(input())

print('Number of pennies after', user_days, 'days: ', end="")
print(double_pennies(starting_pennies, user_days))

 
 
### CS 219T: Python Programming

#### Section 3.5: Creating Recursion

---

### 3.5.1: Recursive Function: Writing the Base Case

**Challenge Activity**

Add an `if` branch to complete the `double_pennies()` base case.

**Sample Output with Inputs: 1 10**

```
Number of pennies after 10 days: 1024
```

**Note:** 
If the submitted code has an infinite loop, the system will stop running the code after a few seconds and report "Program end never reached." The system doesn't print the test case that caused the reported message.

---

**Code:**

```python
# Returns number of pennies if pennies are doubled num_days times
def double_pennies(num_pennies, num_days):
    total_pennies = 0
    
    if ((num_pennies <= 0) or (num_days == 0)):
        total_pennies = num_pennies
    else:
        total_pennies = double_pennies((num_pennies * 2), (num_days - 1))
    
    return total_pennies

# Program computes pennies if you have 1 penny today, 
# 2 pennies after one day, 4 after two days, and so on
starting_pennies = int(input())
user_days = int(input())

print('Number of pennies after', user_days, 'days: ', end='')
print(double_pennies(starting_pennies, user_days))
```

**Explanation:**
- This Python program calculates the number of pennies if the amount is doubled each day for a specified number of days.
- The `double_pennies()` function uses recursion to double the pennies. It includes a base case that stops the recursion when the number of days is 0, returning the current number of pennies.
- The function is tested with user inputs for the starting number of pennies and the number of days.

**Graphical Representation:**

*(Note: Since the presented image doesn't contain any graphs or diagrams, this section is not applicable.)*
Transcribed Image Text:### CS 219T: Python Programming #### Section 3.5: Creating Recursion --- ### 3.5.1: Recursive Function: Writing the Base Case **Challenge Activity** Add an `if` branch to complete the `double_pennies()` base case. **Sample Output with Inputs: 1 10** ``` Number of pennies after 10 days: 1024 ``` **Note:** If the submitted code has an infinite loop, the system will stop running the code after a few seconds and report "Program end never reached." The system doesn't print the test case that caused the reported message. --- **Code:** ```python # Returns number of pennies if pennies are doubled num_days times def double_pennies(num_pennies, num_days): total_pennies = 0 if ((num_pennies <= 0) or (num_days == 0)): total_pennies = num_pennies else: total_pennies = double_pennies((num_pennies * 2), (num_days - 1)) return total_pennies # Program computes pennies if you have 1 penny today, # 2 pennies after one day, 4 after two days, and so on starting_pennies = int(input()) user_days = int(input()) print('Number of pennies after', user_days, 'days: ', end='') print(double_pennies(starting_pennies, user_days)) ``` **Explanation:** - This Python program calculates the number of pennies if the amount is doubled each day for a specified number of days. - The `double_pennies()` function uses recursion to double the pennies. It includes a base case that stops the recursion when the number of days is 0, returning the current number of pennies. - The function is tested with user inputs for the starting number of pennies and the number of days. **Graphical Representation:** *(Note: Since the presented image doesn't contain any graphs or diagrams, this section is not applicable.)*
## Python Recursion: Doubling Pennies

### Problem Statement
The goal is to create a Python function that returns the number of pennies if the pennies are doubled after a certain number of days. Specifically, starting with a given number of pennies, the pennies are doubled every day for a specified number of days.

### Sample Output
```
Number of pennies after 10 days: 1024
```

**Note:**
- If the submitted code has an infinite loop, the system will stop running the code after a few seconds, and report "Program end never reached." The system doesn't print the test case that caused the reported message.

### Python Code
```python
# Returns number of pennies if pennies are doubled num_days times
def double_pennies(num_pennies, num_days):
    total_pennies = 0
    
    # Base case, if no days are left, return the current count of pennies
    if num_days == 0:
        total_pennies = num_pennies
    
    # Recursive case, double the pennies and reduce the number of days
    else:
        total_pennies = double_pennies((num_pennies * 2), (num_days - 1))
    
    return total_pennies

# Program computes pennies if you have 1 penny today,
# 2 pennies after one day, 4 after two days, and so on.
starting_pennies = int(input())
user_days = int(input())

print('Number of pennies after', user_days, 'days: ', end='')
print(double_pennies(starting_pennies, user_days))
```

### Explanation
1. **Function Definition**:
   - The `double_pennies` function takes two arguments: `num_pennies` and `num_days`.
   - A base case is defined where if `num_days` is 0, the function returns the current count of pennies (`num_pennies`).
   - A recursive case is defined to call the function again, doubling the number of pennies and decrementing the number of days by 1.

2. **Main Program**:
   - The user inputs the starting number of pennies and the number of days to double the pennies.
   - The `double_pennies` function is called with the user-provided inputs.
   - The result is printed, showing the number of pennies after the specified number of days.

### Error
Transcribed Image Text:## Python Recursion: Doubling Pennies ### Problem Statement The goal is to create a Python function that returns the number of pennies if the pennies are doubled after a certain number of days. Specifically, starting with a given number of pennies, the pennies are doubled every day for a specified number of days. ### Sample Output ``` Number of pennies after 10 days: 1024 ``` **Note:** - If the submitted code has an infinite loop, the system will stop running the code after a few seconds, and report "Program end never reached." The system doesn't print the test case that caused the reported message. ### Python Code ```python # Returns number of pennies if pennies are doubled num_days times def double_pennies(num_pennies, num_days): total_pennies = 0 # Base case, if no days are left, return the current count of pennies if num_days == 0: total_pennies = num_pennies # Recursive case, double the pennies and reduce the number of days else: total_pennies = double_pennies((num_pennies * 2), (num_days - 1)) return total_pennies # Program computes pennies if you have 1 penny today, # 2 pennies after one day, 4 after two days, and so on. starting_pennies = int(input()) user_days = int(input()) print('Number of pennies after', user_days, 'days: ', end='') print(double_pennies(starting_pennies, user_days)) ``` ### Explanation 1. **Function Definition**: - The `double_pennies` function takes two arguments: `num_pennies` and `num_days`. - A base case is defined where if `num_days` is 0, the function returns the current count of pennies (`num_pennies`). - A recursive case is defined to call the function again, doubling the number of pennies and decrementing the number of days by 1. 2. **Main Program**: - The user inputs the starting number of pennies and the number of days to double the pennies. - The `double_pennies` function is called with the user-provided inputs. - The result is printed, showing the number of pennies after the specified number of days. ### Error
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

After working this code, this is the error code that popped up resulted in half right.   Traceback (most recent call last): File "main.py", line 292, in <module> print(double_pennies(starting_pennies, user_days)) File "main.py", line 264, in double_pennies total_pennies = double_pennies((num_pennies * 2), (num_days - 1)) File "main.py", line 264, in double_pennies total_pennies = double_pennies((num_pennies * 2), (num_days - 1)) File "main.py", line 264, in double_pennies total_pennies = double_pennies((num_pennies * 2), (num_days - 1)) [Previous line repeated 995 more times] File "main.py", line 260, in double_pennies if num_days == 1: RecursionError: maximum recursion depth exceeded in comparison

### Creating Recursion for Doubling Pennies

#### Objective
To teach how to create a recursive function that computes the number of pennies when they are doubled for a given number of days.

#### Description

Below is a Python code snippet that demonstrates a recursive function called `double_pennies`. This function returns the number of pennies if they are doubled a specified number of times (`num_days`).

```python
# Returns number of pennies if pennies are doubled num_days times
def double_pennies(num_pennies, num_days):
    total_pennies = 0
    if num_days == 1:
        return num_pennies * 2
    else:
        total_pennies = double_pennies((num_pennies * 2), (num_days - 1))

    return total_pennies

# Program computes pennies if you have 1 penny today,
# 2 pennies after one day, 4 after two days, and so on
starting_pennies = int(input())
user_days = int(input())

print('Number of pennies after', user_days, 'days: ', end="")
print(double_pennies(starting_pennies, user_days))
```

#### Explanation

1. **double_pennies function:**
    - **Parameters:**
        - `num_pennies`: Initial number of pennies.
        - `num_days`: Number of days the pennies are doubled.
    - **Logic:**
        - If `num_days` is 1, it returns the number of pennies doubled (`num_pennies * 2`).
        - Otherwise, it calls itself recursively with `num_pennies` doubled and `num_days` decremented by 1.
    - **Return:**
        - Returns the total number of pennies after doubling for the specified duration.

2. **Main Program:**
    - **Inputs:**
        - `starting_pennies`: Initial number of pennies entered by the user.
        - `user_days`: Number of days entered by the user.
    - **Output:**
        - Displays the number of pennies after the given days using the `double_pennies` function.

#### Diagram

- **Flowchart:**

  1. Start
  2. Input `starting_pennies` and `user_days`
  3. Call `double_pennies` with `starting_pennies` and `user_days
Transcribed Image Text:### Creating Recursion for Doubling Pennies #### Objective To teach how to create a recursive function that computes the number of pennies when they are doubled for a given number of days. #### Description Below is a Python code snippet that demonstrates a recursive function called `double_pennies`. This function returns the number of pennies if they are doubled a specified number of times (`num_days`). ```python # Returns number of pennies if pennies are doubled num_days times def double_pennies(num_pennies, num_days): total_pennies = 0 if num_days == 1: return num_pennies * 2 else: total_pennies = double_pennies((num_pennies * 2), (num_days - 1)) return total_pennies # Program computes pennies if you have 1 penny today, # 2 pennies after one day, 4 after two days, and so on starting_pennies = int(input()) user_days = int(input()) print('Number of pennies after', user_days, 'days: ', end="") print(double_pennies(starting_pennies, user_days)) ``` #### Explanation 1. **double_pennies function:** - **Parameters:** - `num_pennies`: Initial number of pennies. - `num_days`: Number of days the pennies are doubled. - **Logic:** - If `num_days` is 1, it returns the number of pennies doubled (`num_pennies * 2`). - Otherwise, it calls itself recursively with `num_pennies` doubled and `num_days` decremented by 1. - **Return:** - Returns the total number of pennies after doubling for the specified duration. 2. **Main Program:** - **Inputs:** - `starting_pennies`: Initial number of pennies entered by the user. - `user_days`: Number of days entered by the user. - **Output:** - Displays the number of pennies after the given days using the `double_pennies` function. #### Diagram - **Flowchart:** 1. Start 2. Input `starting_pennies` and `user_days` 3. Call `double_pennies` with `starting_pennies` and `user_days
**Recursive Function Example with Pennies Doubling**

**Testing with inputs: 1 10**
- **Your output:** Number of pennies after 10 days: 1024

**Testing with inputs: 1 40**
- **Your output:** Number of pennies after 40 days: 1099511627776

**Testing with inputs: 1 1**
- **Your output:** Number of pennies after 1 day: 2

**Test aborted**
- **Error Details:**
  ```
  File "main.py", line 292, in <module>
    print(double_pennies(starting_pennies, user_days))
  File "main.py", line 264, in double_pennies
    total_pennies = double_pennies((num_pennies * 2), (num_days - 1))
  File "main.py", line 264, in double_pennies
    total_pennies = double_pennies((num_pennies * 2), (num_days - 1))
  File "main.py", line 264, in double_pennies
    total_pennies = double_pennies((num_pennies * 2), (num_days - 1))
  ...
  [Previous line repeated 995 more times]
  File "main.py", line 260, in double_pennies
  ```

The error log shows that the program encountered a "recursion depth exceeded" error. This suggests that the recursive function `double_pennies` called itself too many times without reaching a base case, causing the program to exceed the maximum recursion depth allowed in Python.
Transcribed Image Text:**Recursive Function Example with Pennies Doubling** **Testing with inputs: 1 10** - **Your output:** Number of pennies after 10 days: 1024 **Testing with inputs: 1 40** - **Your output:** Number of pennies after 40 days: 1099511627776 **Testing with inputs: 1 1** - **Your output:** Number of pennies after 1 day: 2 **Test aborted** - **Error Details:** ``` File "main.py", line 292, in <module> print(double_pennies(starting_pennies, user_days)) File "main.py", line 264, in double_pennies total_pennies = double_pennies((num_pennies * 2), (num_days - 1)) File "main.py", line 264, in double_pennies total_pennies = double_pennies((num_pennies * 2), (num_days - 1)) File "main.py", line 264, in double_pennies total_pennies = double_pennies((num_pennies * 2), (num_days - 1)) ... [Previous line repeated 995 more times] File "main.py", line 260, in double_pennies ``` The error log shows that the program encountered a "recursion depth exceeded" error. This suggests that the recursive function `double_pennies` called itself too many times without reaching a base case, causing the program to exceed the maximum recursion depth allowed in Python.
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Returning value from Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education