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
Len [333]
2 years ago
11

Write the function definition for a function called list_total that accepts a list of integers

Computers and Technology
1 answer:
uranmaximum [27]2 years ago
7 0

Answer:

def list_total(numbers):

   sum_of_numbers = 0

   for number in numbers:

       sum_of_numbers += number

   return sum_of_numbers

Explanation:

So, to define a function, the syntax is simply:

def functionName(arguments here):

   # code

So to define a function called "list_total" which accepts a list of integers, you write:

"

def list_total(numbers):
   # code

"

any the "numbers" is a parameter, and it's just like any variable, so you can name it anything besides keywords. I just named it "numbers" since it makes sense in this context, you could also write "integers" instead and it would be just as valid, and may be a bit more specific in this case.

Anyways from here, the initial sum should be equal to 0, and from there we add each number in the list to that initial sum. This can be done by initializing a variable to the value "0" and then traversing the list using a for loop. This can be done as such:

"

def list_total(numbers):

   sum_of_numbers = 0

   for number in numbers:

       # code

"

So for each element or integer in the list "numbers" the for lop will run, and the variable "number" will contain the value of the current element it's on.

So we can add the "number" to the sum_of_numbers as such:

sum_of_numbers = sum_of_numbers + number

but there is a shorter way to do this, and it's represented as:

sum_of_numbers += sum_of_numbers

which will do the same exact thing. In fact this works for any mathematical operation for example:

a *= 3

is the same thing as

a = a * 3

and

a /= 3

is the same thing as

a = a / 3

It's the same thing, but it's a much shorter and more readable notation.

Anyways, this code will go in the for loop to almost finish the code

"

def list_total(numbers):

   sum_of_numbers = 0

   for number in numbers:

       sum_of_numbers += number

"

The last thing is to return this value, and this is simply done by using the syntax:

"return {value here}"

and since the sum_of_numbers has the value, we write

"return sum_of_numbers"

at the end of the function, which is very very important, because when you use the return keyword, you end the function, and return whatever value is next to the return to the function call

So to finish the code we just add this last piece to get:

"

def list_total(numbers):

   sum_of_numbers = 0

   for number in numbers:

       sum_of_numbers += number

   return sum_of_numbers

"

You might be interested in
You are going to write a program for Computer test which will read 10 multiple choice questions from a file, order them randomly
sweet-ann [11.9K]

Answer:

Code is too large , i attached a source file below and also a text file from where i get Questions

Explanation:

6 0
3 years ago
Effective nonverbal communication can cause tension.<br><br> True or False
agasfer [191]

Explanation:

When your nonverbal signals match up with the words you're saying, they increase trust, clarity, and rapport. When they don't, they can generate tension, mistrust, and confusion.

4 0
3 years ago
Write a class named Book containing: Two instance variables named title and author of type String. A constructor that accepts tw
andrey2020 [161]

Answer:

The code to this question can be given as:

Code:

public class Book  //define class.  

{

private String title, author;   //define variable.

Book(String a, String b)  //define parameterized constructor.

{

title = a;   //holding value.

author = b;

}

public String toString()  //string function

{

return title + "\n" + author;    //return value.

}

}

Explanation:

In the above java code firstly we declare the class book that name is already given in the question. Then we declare the private variable that datatype is string author and title. Then we declare the parameterized constructor. In the parameterized constructor we use the private variable for hold constructor parameter value. Then we declare the tostring() function in this function we return value of the title and author variable.

3 0
3 years ago
A large gambling company needs to be able to accept high volumes of customer wagers within short timeframes for high-profile spo
Dima020 [189]

Answer: A. a mobile app that only accepts wagers based on the user's location

Explanation:

Elastic cloud simply refers to the cloud computing offering which due to the need for change helps in the provision of variable service levels.

Based on the information given in the question, an effective elastic Cloud solution that can meet this client’s needs is a mobile app that only accepts wagers based on the user's location.

Since there are strict laws that prohibit gambling activities outside the licensed zones, the user location is vital in the mobile app.

5 0
3 years ago
Agriscience in the United States?
Mrac [35]

Answer:

The history of agriculture in the United States covers the period from the first English settlers to the present day. In Colonial America, agriculture was the primary livelihood for 90% of the population, and most towns were shipping points for the export of agricultural products. Most farms were geared toward subsistence production for family use. The rapid growth of population and the expansion of the frontier opened up large numbers of new farms, and clearing the land was a major preoccupation of farmers. After 1800, cotton became the chief crop in southern plantations, and the chief American export. After 1840, industrialization and urbanization opened up lucrative domestic markets. The number of farms grew from 1.4 million in 1850, to 4.0 million in 1880, and 6.4 million in 1910; then started to fall, dropping to 5.6 million in 1950 and 2.2 million in 2008

Explanation:

8 0
3 years ago
Other questions:
  • Interactive sites where users write personal topics and comments to a threadded discussion are called?
    11·1 answer
  • Within an event-driven program a component from which an event is generated is the ____
    10·1 answer
  • A______ is a graphical representation of numeric data.
    8·1 answer
  • Read the following sentence from an argumentative essay.
    11·1 answer
  • In Modern operating system, the __ feature has dramatically improved user productivity.
    11·1 answer
  • You are asked to monitor a network which has a network monitoring system. Using the monitoring server, you notice that it is lis
    13·1 answer
  • What is the first step in creating a maintenance ?
    7·1 answer
  • You are a running cable in a new network. You are running a series of cables from the wiring closet to the RJ-45 ports for the c
    6·1 answer
  • Help pweeze this is due today :(<br><br> I will give u brainliest just pweeze, I need this answer :(
    8·1 answer
  • A database that is created and maintained using services such as Microsoft Azure or Amazon AWS is called a(n) _____ database.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!