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 17:40
Consider the simple 3-station assembly line illustrated below, where the 2 machines at station 1 are parallel, i.e., the product only needs to go through one of the 2 machines before proceeding to station 2.what is the throughput time of this process?
Answers: 2
question
Computers and Technology, 23.06.2019 17:20
What is the best assassins creed game?
Answers: 2
question
Computers and Technology, 24.06.2019 15:30
The idea that, for each pair of devices v and w, there’s a strict dichotomy between being “in range” or “out of range” is a simplified abstraction. more accurately, there’s a power decay function f (·) that specifies, for a pair of devices at distance δ, the signal strength f(δ) that they’ll be able to achieve on their wireless connection. (we’ll assume that f (δ) decreases with increasing δ.) we might want to build this into our notion of back-up sets as follows: among the k devices in the back-up set of v, there should be at least one that can be reached with very high signal strength, at least one other that can be reached with moderately high signal strength, and so forth. more concretely, we have values p1 ≥ p2 ≥ . . ≥ pk, so that if the back-up set for v consists of devices at distances d1≤d2≤≤dk,thenweshouldhavef(dj)≥pj foreachj. give an algorithm that determines whether it is possible to choose a back-up set for each device subject to this more detailed condition, still requiring that no device should appear in the back-up set of more than b other devices. again, the algorithm should output the back-up sets themselves, provided they can be found.\
Answers: 2
question
Computers and Technology, 24.06.2019 17:40
Anewly established internet company with 40 employees needs your advice. they are looking for a collaboration tool and have narrowed their choices to gotomeeting, webex, and my web conferences. after reading the information presented in this chapter and other sources, prepare a two- page document ( double spaced) that includes two advantages and two disadvantages of each tool. which one is your final recommendation? why did you choose that tool over the other two?
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, 06.11.2020 20:50
question
Mathematics, 06.11.2020 20:50
question
Mathematics, 06.11.2020 20:50
question
Biology, 06.11.2020 20:50
question
Mathematics, 06.11.2020 20:50
question
Mathematics, 06.11.2020 20:50
question
Mathematics, 06.11.2020 20:50
Questions on the website: 13722367