Computers and Technology
Computers and Technology, 22.05.2021 06:20, vari1738

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.

answer
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 20:40, aurikmah2005
Instruction active describing list features which statements accurately describe the features of word that are used to create lists? check all that apply. the tab key can be used to create a sublist. the enter key can be used to add an item to a list. the numbering feature allows for the use of letters in a list. the numbering feature can change the numbers to bullets in a list. the multilevel list feature provides options for different levels in a list.
Answers: 2
image
Computers and Technology, 24.06.2019 02:20, isabelsmhl
Peter is thinking of a number which isless than 50. the number has 9 factors. when he adds 4 to the number, itbecomes a multiple of 5. what is thenumber he is thinking of ?
Answers: 1
image
Computers and Technology, 24.06.2019 22:10, gabriellabadon2
In command prompt, whats a command that will list only .ini files in c: \windows\system32 directory?
Answers: 1
image
Computers and Technology, 25.06.2019 09:50, ccory0626
In any one-minute interval, the number of requests for a popular web page is a poisson random variable with expected value 240 requests. (a) a web server has a capacity of requests per minute. if the number of requests in a one-minute interval is greater than the server is overloaded. use the central limit theorem to estimate the smallest value of for which the probability of overload is less than 0.035. note that your answer must be an integer. also, since this is a discrete random variable, don't forget to use "continuity correction". = 268 (b) now assume that the server's capacity in any one-second interval is ⌊/60⌋, where ⌊⌋ is the largest integer ≤ (this is called the floor function.) for the value of derived in part (a), what is the probability of overload in a one-second interval? this time, don't approximate via the clt, but compute the probability exactly.
Answers: 2
Do you know the correct answer?
One of the biggest benefits of writing code inside functions is that we can reuse the code. We simpl...

Questions in other subjects:

Konu
Mathematics, 10.12.2020 20:30
Konu
Mathematics, 10.12.2020 20:30