UNIT 1

Activity 2
Complete all components in the activity below according to the given instructions. Refer to the provided rubric for information on how you will be graded. Submit your work as a file attachment using the dropbox.

The activity is worth 15 points.

UNIT 1 ACTIVITY 2
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)
Now it’s your turn to do the following:

Type all of the original code into a new file in OnlineGDB.
Copy the add function from the unit and paste it at the top of your program.
Write three additional functions: subtract, multiply, and divide. Pay careful attention to the parameters and return statement. Remember to put the three functions at the top of your Python program before your main code.
Rewrite the main code so that your functions are called.
Part B
There are many different ways that a user could tell us that they would like to add two numbers in our calculator program. The user could type “add”, “Add”, “ADD”, or “+”, to name a few possibilities. Of course, as humans, we know exactly what is meant, even if the word is capitalized. But the Python Interpreter can’t tell that “add” is the same as “Add”.

We can use a list to make our program a bit more robust. We can also use the IN operator to check for certain values in that list. Take a look at this if statement’s opening line:

if operation in ["add", "Add", "ADD", "+"]:
Make those changes in your program and verify that it works.
Consider all of the possible words the user might enter to subtract, multiply, or divide.
Rewrite the first lines of each of your if statements to use lists.
Thoroughly test your new program, trying out each of the four operations.
When you have tested your program, click the save button. Then click Share and copy the program link. Paste the link in a word processing document and submit using this unit’s dropbox. If your program does not work properly, also include a paragraph explaining what you did to troubleshoot it.
TABLE Grading Rubric
Part A Part B
Full Credit
Three additional functions (subtract, multiply, and divide) have been written correctly. They have also been called correctly from the main program.

Lists have been used for all of the if statements, and at least four different items are in each list. The in operator has been used correctly, and the program runs smoothly.

Partial Credit
An attempt has been made to write the three additional functions, but they may not have used parameters or the return statement correctly. There may be a problem with the function calls.

There has been an attempt to use lists for most of the if statements. They may contain fewer than four items.

Little Credit
No additional functions have been written or called. There may be syntax errors that prevent the program from running.

Lists have not been used. The program may not run because of syntax errors.

pls help

Sure, I can help you with this activity. Please let me know specifically what you need assistance with.