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
Help, please!! A file named "games.txt" exists and has 80 lines of data. You open the file with the following line of code. aFil
Aliun [14]
I’m not 100% sure about this but I think it is A
8 0
4 years ago
someone please tell me if you watch drag race (rupauls drag race) I need someone to talk to about it ​
lys-0071 [83]
I’m gonna watch it soon
3 0
3 years ago
R7.1 Carry out the following tasks with an array:
Andru [333]

Answer:

Following are the program in the C++ Programming Language.

//set header file or namespace

#include <iostream>

using namespace std;

//define main function

int main() {

//set integer type array with indexing 10

 int a[10];

//set integer type variable to 1

 int i=1;

//set element in 1st index

 a[0]=17;

//set element in last index

 a[9]=29;

//set while loop for the remaining elements

 while(i<9)

 {

//set -1 in the remaining elements

   a[i]=-1;

   i++;

 }

//set for loop to print array

 for ( int j = 0; j < 10; j++ ) {

   cout << a[j]<<endl;

 }

}

<u>Output:</u>

17

-1

-1

-1

-1

-1

-1

-1

-1

29

Explanation:

In the following program, we define the main function "main()" and inside it.

  • Set an integer type array element "a[]" with index value 10.
  • Set integer data type variable "i" initialize to 1.
  • Set elements in the first and last place in the array.
  • Set the while loop to initialize elements for the remaining place.
  • Set the for loop to print the array elements.
6 0
3 years ago
Susan needs to change the color scheme in all the slides of her multimedia presentation. The presentation software program Susan
siniylev [52]
I believe this would be C, B, A, D
Sorry if it wasn’t.
4 0
3 years ago
Read 2 more answers
Which functions are available in a Word table? Check all that apply.
dexar [7]
All of them are available
3 0
3 years ago
Other questions:
  • The goal of conducting an incident analysis is to ascertain weakness. Because each incident is unique and might necessitate a di
    8·1 answer
  • Select the tips you should follow when creating a newsletter.
    13·1 answer
  • Jenny needs to record the names of 30 students, write down the subjects they studied, and note grades in each subject after the
    13·2 answers
  • Server 2016 is compatible for Powershell 5.0 State True or False.
    7·1 answer
  • Why should you avoid the use of sarcasm, clichés, and idioms in business letters?
    11·2 answers
  • What is the output of the first and second print statements?
    13·1 answer
  • Convert the following denary numbers into binary using 8-bit register:
    10·1 answer
  • What steps can you take to secure your private information?.
    8·1 answer
  • OCR Airlines allows passengers to carry up to 25kg of luggage free of charge. Any additional luggage is charged at £10 per kg. N
    8·1 answer
  • Which of the following commands can be used to display socket information out to the terminal screen
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!