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]
1 year 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]1 year 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
Carlos, an algebra teacher, is creating a series of PowerPoint presentations to use during class lectures. After writing, format
yarga [219]

Answer:

see explanation

Explanation:

Carlos can make a copy of the old presentation that preserves all the formatting, and replace old content with new information.

5 0
3 years ago
What command will unregister a component when run?
Advocard [28]
<span>Regsvr32.exe/u will unregister a component when run. Regsvr32.exe/u is a utility tool that is installed on Windows Xp or later versions of Windows. It has a 32-bit version and 64-bit version which can be found in the system root folder on a computer. It is capable of unregistering servers.</span>
7 0
3 years ago
Read 2 more answers
What is sexual intercourse <br><br>​
n200080 [17]

Answer:

immmm ....

I know the answer but I won't tell..lolllll

7 0
2 years ago
Read 2 more answers
Make a class Employee with a name and salary. Make a class Manager inherit from Employee. Add an instance variable, named depart
sweet [91]

Answer:

see explaination

Explanation:

class Employee

{

String name;

double salary;

void tostring()

{

System.out.println("Employee:\nName: "+name+"\nSalary: "+salary+"\n");

}

Employee(String n,double s)

{

name=n;

salary=s;

}

}

class Manager extends Employee

{

String department;

void tostring()

{

System.out.println("Manager:\nName: "+name+"\nDepartment: "+department+"\nSalary: "+salary+"\n");

}

Manager(String n,double s,String d)

{

super(n,s);

department=d;

}

}

class Executive extends Manager

{

void tostring()

{

System.out.println("Executive:\nName: "+name+"\nDepartment: "+department+"\nSalary: "+salary+"\n");

}

Executive(String n,double s,String d)

{

super(n,s,d);

}

}

public class test

{

public static void main(String args[])

{

Employee e =new Employee("Neo",12000);

e.tostring();

Manager m =new Manager("Cramster",100000,"Homework");

m.tostring();

Executive ex =new Executive("Chuks",1000000,"Homework");

ex.tostring();

}

}

8 0
3 years ago
Help ME! Will Mark BRAINLIEST! Its Engineering!
Anettt [7]
What is it about ? And what you need help on
5 0
3 years ago
Other questions:
  • Which best compares and contrasts visual and performing arts? Both careers use communication skills; however, people involved in
    15·2 answers
  • Use the variables k and total to write a while loop that computes the sum of the squares of the first 50 counting numbers, and a
    10·1 answer
  • If you are feeling sick and you want to drive somewhere, you should
    12·2 answers
  • Whether you work in m-commerce, e-commerce, or brick-and-mortar commerce, to be successful you will need to be able to think cri
    15·1 answer
  • Balance is the design principle that is represented when using the Crop tool?
    6·1 answer
  • What is a table in excel
    10·2 answers
  • What is an information technology? ​
    12·2 answers
  • Stock Market
    11·1 answer
  • Tinh T = a1*a2*a3*...an<br><br> Giúp em với
    13·1 answer
  • What is the difference between a computer’s RAM and its hard disk?
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!