Welcome to Westonci.ca, where your questions are met with accurate answers from a community of experts and enthusiasts. Get detailed and accurate answers to your questions from a dedicated community of experts on our Q&A platform. Our platform offers a seamless experience for finding reliable answers from a network of knowledgeable professionals.

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 REPL.it.Copy the add function from the unit and paste it at the top of your program.Write 3 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.

Sagot :

The program is an illustration of functions

What are functions?

Functions are collections of code segments, that are executed when called or evoked

The program

The program in Python, where comments are used to explain each line is as follows

#This defines the add function

def add(num1,num2):

   return(num1, "+", num2,"=", num1 + num2)

#This defines the subtract function

def subtract(num1,num2):

   return(num1, "-", num2,"=", num1 - num2)

#This defines the multiply function

def multiply(num1,num2):

   return(num1, "*", num2,"=", num1 * num2)

#This defines the divide function

def divide(num1,num2):

   return(num1, "/", num2,"=", num1 / num2)

#The main method begins here

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(add(num1,num2))

elif operation == "subtract":

   print(subtract(num1,num2))

elif operation == "multiply":

   print(multiply(num1,num2))

elif operation == "divide":

   print(divide(num1,num2))

else:

   print("Not a valid operation.")

Read more about functions at:

https://brainly.com/question/14284563

Thank you for your visit. We're committed to providing you with the best information available. Return anytime for more. We hope you found this helpful. Feel free to come back anytime for more accurate answers and updated information. Find reliable answers at Westonci.ca. Visit us again for the latest updates and expert advice.