1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Sergeu [11.5K]
2 years ago
11

One of the biggest benefits of writing code inside functions is that we can reuse the code. We simply call it whenever we need i

t!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.
Computers and Technology
1 answer:
Fantom [35]2 years ago
5 0

The program is an illustration of functions

<h3>What are functions?</h3>

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

<h3>The program</h3>

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:

brainly.com/question/14284563

You might be interested in
What is the benefit of using pre-programmed functions to analyze data?"
Murrr4er [49]

Answer:

A pre-programmed function is a section of code which is reusable to perform certain routine. One benefit of using a pre-programmed function to analyze data is the code redundancy can be reduced. The function is only written for once and it can be called to perform a specific data analysis whenever it is needed.

Besides, a pre-programmed function also offer consistent analytical output as all data are processed by a same analytical procedure in the function.  

5 0
2 years ago
________ refers to the ability to model components and show how the components' inputs and outputs relate to one another.
Black_prince [1.1K]

Answer:

Central Processing Unit

Explanation:

When the CPU puts the address of a peripheral onto the address bus, the input–output interface decodes the address and identifies the unique computer peripheral with which a data transfer operation is to be executed.

5 0
3 years ago
What is a Web application?
OLga [1]

Answer:

A web application is a computer program that utilizes web browsers and web technology to perform tasks over the Internet.

Explanation:

Web applications include online forms, shopping carts, word processors, spreadsheets, video and photo editing, file conversion e.t.c.

7 0
3 years ago
Your organization has hired a penetration tester to validate the security of your environment. The penetration tester needs to d
yulyashka [42]

Answer:

Port scanner

Explanation:

The penetration tester would most likely use a port scanner. A port scanner can be explained to be an application that is made to check a server or host for ports that are open. This application can also be used by administrators to check their security networks so as to know those network services that are running on a host and also to know existing vulnerabilities. Attackers also use this to exploit victims.

5 0
3 years ago
ven int variables k and total that have already been declared, use a while loop to compute the sum of the squares of the first 5
monitta

Answer:

Following are statement is given below

int  k=1,total=0; // variable declaration

while(k<50) // iterating the while loop

{

   total=total+k*k;//  calculating the square

   k=k+1; // increments the value of k by 1    

}

Explanation:

Following are the description of Statement.

  • Declared a variable "total" and "k" of the "integer " type initialized the total to 0 and "k" to 1.
  • Iterating the while loop for less then 50 .In this loop, we calculating the sum of square of first 50 number in the "total" variable .
  • After that increment the value of "k" variable by 1 to execute the loop less then 50 .

3 0
3 years ago
Other questions:
  • Kendra needs to configure a visual alert for an appointment that she is creating for next week. What should she configure?
    11·1 answer
  • Marie uses a browser to visit a blog. What is the unique identifier of the blog?
    13·2 answers
  • Which of the following contains information about which keywords are bringing visitors to your website?
    8·1 answer
  • Cisco Next Generation Intrusion Prevention System (NGIPS) devices include global correlation capabilities that utilize real-worl
    9·1 answer
  • The parts of a memo are _____.
    9·2 answers
  • A reference parameter differs from an output parameter in that a reference parameter ______________________ but an output parame
    5·1 answer
  • CHOOSE THE CORRECT CONTINUOUS TENSES( PRESENT CONTINUOUS,PAST CONTINUOUS AND FUTURE CONTINUOUS) : 1. I saw a snake while I _____
    10·1 answer
  • Implemente a função ao lado, que recebe um preço e um booleano indicando se já está com desconto ou não. Se o preço for maior qu
    8·1 answer
  • Why do you want to work for Rev?
    5·1 answer
  • Write a Pascal program that will prompt the user to enter the radius of a circle.
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!