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
Spencer wants to choose a career path that wouldn’t hold him to an office or laboratory. Which of the following career paths wou
ruslelena [56]
Photography is the best choice but video game designers are also nice
please mark as brainliest
 
5 0
3 years ago
Read 2 more answers
Put the steps you would use to insert and label an endnote in order.
Sholpan [36]

Answer:

2nd- step 1, 3rd- step 2, 1st- step 3, 4th- step 4

Explanation:

1. locate the reference tab

2. click the insert endnote option

3. type your endnote label

4. type the text you want the endnote to follow

3 0
3 years ago
Marie wanted to make sure that the information in her presentation was well organized. She should _____.
Sauron [17]
Marie could either ask a friend or her parents to peer edit it and tell her if its good enough or if she should work on it more.<span />
4 0
3 years ago
Bob flys a drone which has a 20 megapixel camera attached, what is the definition of "megapixel in this context? Why does it mat
Verizon [17]

Answer:

megapixel refers to the unit of resolution i.e. one million

Explanation:

Interestingly the higher the pixels does not mean higher quality of image, it's more about the camera and it's sensor.

I am a photographer and a licensed drone pilot and have researched the subject to help with camera choice, both DSLR and drone.

5 0
3 years ago
Chris wants to view a travel blog her friend just created. Which tool will she use?
wolverine [178]

Answer:

Web browser

Explanation:

7 0
3 years ago
Other questions:
  • Which structural adaptation would help a plant survive better in a shady environment?
    14·2 answers
  • Spencer is a recent graduate who finds a dream car that costs $45,000. Even better, there's a 0% APR for 36 months offer from th
    13·1 answer
  • Arpanet was developed by the united states so that there was a communication network that would survive a nuclear war. True or F
    11·1 answer
  • A(n) _______ is text or a graphic you can click to jump to another file or to somewhere else in the same file. quizle
    12·1 answer
  • Describe what a structure is in a living thing?
    9·1 answer
  • Which is the output of the formula =NOT(12+12=24) ?
    7·1 answer
  • Complete the below function which dynamically allocates space to a 3d array of doubles, initializes all values to 0, and returns
    8·1 answer
  • Excel can display characters in only three font colors: black, red, and blue. (points : 2) true false
    11·1 answer
  • Which of the following terms describes a product that is designed to meet humans’ physical and/or psychological needs?
    12·2 answers
  • List two major problems that could be addressed by futuring.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!