subject

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.

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 02:00
Aletter or menu command that starts an action when the user presses the designated letter and the alt key together is called what?
Answers: 1
question
Computers and Technology, 23.06.2019 13:30
Best laptops for college [$100-$500 range]?
Answers: 2
question
Computers and Technology, 24.06.2019 08:30
Intellectual property rights are exclusive rights that protect both the created and the creation. ipr offers exclusively what benefits to the person or people covered by it
Answers: 3
question
Computers and Technology, 24.06.2019 13:30
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. the program should output the average high, average low, and the highest and lowest temper- atures for the year. your program must consist of the following functions: a. function getdata: this function reads and stores data in the two- dimensional array. b. function averagehigh: this function calculates and returns the average high temperature for the year. c. function averagelow: this function calculates and returns the aver- age low temperature for the year. d. function indexhightemp: this function returns the index of the highest high temperature in the array. e. function indexlowtemp: this function retur
Answers: 3
You know the right answer?
One of the biggest benefits of writing code inside functions is that we can reuse the code. We simpl...
Questions
question
History, 08.03.2020 23:58
question
Mathematics, 08.03.2020 23:59
question
History, 08.03.2020 23:59
question
Social Studies, 09.03.2020 00:00
Questions on the website: 13722360