i need help with UNIT 1 ACTIVITY 2 these are the ruls

Reduce Re-use and Recycle!
Required Materials
OnlineGDB (login required)
Word processing software
Part A
One of the biggest benefits of writing code inside functions is that we can reuse the code. We simply call it whenever we need it!

Let’s take a look at a calculator program that could be rewritten in a more reusable way with functions. Notice that two floats (decimal numbers, but they can also include integers) are inputted by the user, as an operation that the user would like to do. A series of if statements are used to determine what operation the user has chosen, and then the answer is printed inside a formatted print statement.

num1 = float(input("Enter your first number: "))
num2 = float(input("Enter your second number: "))
operation = input("What operation would you like to do? Type add, subtract, multiply, or divide.")
if operation == "add":
print(num1, "+", num2,"=", num1 + num2)
elif operation == "subtract":
print(num1, "-", num2,"=", num1 - num2)
elif operation == "multiply":
print(num1, "*", num2,"=", num1 * num2)
elif operation == "divide":
print(num1, "/", num2,"=", num1 / num2)
else:
print("Not a valid operation.")
Your job is to rewrite the program using functions. We have already looked at a function that adds two numbers. Using that as a starting point, we could call the add function from within our program in this way:

if operation == "add":
result = add(num1, num2)
print(num1, "+", num2,"=",result)

1 answer

Here is the rewritten program using functions:

```python
# Function to add two numbers
def add(num1, num2):
return num1 + num2

# Function to subtract two numbers
def subtract(num1, num2):
return num1 - num2

# Function to multiply two numbers
def multiply(num1, num2):
return num1 * num2

# Function to divide two numbers
def divide(num1, num2):
return num1 / num2

# Main program
num1 = float(input("Enter your first number: "))
num2 = float(input("Enter your second number: "))
operation = input("What operation would you like to do? Type add, subtract, multiply, or divide.")

if operation == "add":
result = add(num1, num2)
print(num1, "+", num2,"=", result)
elif operation == "subtract":
result = subtract(num1, num2)
print(num1, "-", num2,"=", result)
elif operation == "multiply":
result = multiply(num1, num2)
print(num1, "*", num2,"=", result)
elif operation == "divide":
result = divide(num1, num2)
print(num1, "/", num2,"=", result)
else:
print("Not a valid operation.")
```

In this rewritten program, four functions `add`, `subtract`, `multiply`, and `divide` are defined to perform the respective operations. The main program then calls the appropriate function based on the user's input and prints the result. This approach makes the code more modular and reusable.
Similar Questions
  1. ReduceA. Gautam A landfill in Hawaii A thrift store You’ve heard the three Rs in reduce, reuse, and recycle. The most
    1. answers icon 1 answer
  2. Analyze Informational Text Unit Test10 of 1510 of 15 Items Question Use the information from the two sources to answer the
    1. answers icon 1 answer
  3. Fix EVERYTHING WRONG WITH THE CODEpython class Chatbox: def self.responses = { "hi": "Hello! How can I assist you with recycling
    1. answers icon 1 answer
    1. answers icon 1 answer
more similar questions