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
(True/False) Utilizing a higher bandwidth can support a larger volume of data (in bits per second) to be transmitted than a lowe
lions [1.4K]

Answer:

True

Explanation:

Bandwidth refers to the maximum data transfer rate across the communication media it refers to the amount of information sent for unit time and it's not related to the speed at which data is transferred (latency) but rather the amount of information. As an analogy imagine a full-pipe, the width of the pipe is related to the amount of water per unit time across the pipe (bandwidth).

3 0
3 years ago
Read 2 more answers
Assignment Background Video games have become an outlet for artists to express their creative ideas and imaginations and a great
Sphinxa [80]

Answer:

Code

import csv

def open_file():

try:

fname=input("Enter the filename to open (xxx.yyy): ")

fp=open(fname,encoding='utf-8')

return fp

except FileNotFoundError:

print("File not found! Please Enter correct filename with extension")

return open_file()

def read_file(fp):

fields = []

rows = []

csvreader = list(csv.reader(fp))

# skiped 1 row because it contains field name

fields = csvreader[0]

D1={}

D2={}

D3={}

# accessing the data of csv file

for line in csvreader[1:]:

name = line[0].lower().strip()

platform = line[1].lower().strip()

if line[2]!='N/A':

year = int(line[2])

else:

year=line[2]

genre = line[3].lower().strip()

publisher = line[4].lower().strip()

na_sales = float(line[5])

eur_sales = float(line[6])

jpn_sales = float(line[7])

other_sales = float(line[8])

global_sales=(na_sales+eur_sales+jpn_sales+other_sales)*1000000

if name not in D1:

D1[name]=[]

D1[name].append((name, platform, year, genre, publisher,global_sales))

if genre not in D2:

D2[genre]=[]

D2[genre].append((genre, year, na_sales, eur_sales,jpn_sales, other_sales, global_sales))

if publisher not in D3:

D3[publisher]=[]

D3[publisher].append((publisher, name, year, na_sales,eur_sales, jpn_sales, other_sales, global_sales))

# sorting

temp={}

# sorting keys

for Keys in sorted (D1) :

ls=D1[Keys]

ls=sorted(ls,key=lambda x: x[-1]) # sorting values

temp[Keys]=ls

D1=temp.copy()

temp={}

# sorting keys

for Keys in sorted (D2) :

ls=D2[Keys]

ls=sorted(ls,key=lambda x: x[-1]) # sorting values

temp[Keys]=ls

D2=temp.copy()

temp={}

# sorting keys

for Keys in sorted (D3) :

ls=D3[Keys]

ls=sorted(ls,key=lambda x: x[-1]) # sorting values

temp[Keys]=ls

D3=temp.copy()

return D1,D2,D3

 

def main():

fp=open_file()

D1,D2,D3=read_file(fp)

# displaying the row_count in dictionaries

cnt=0

for d in D1:

cnt+=1

print(cnt)

print("\n\n=============================================\n\n")

cnt=0

for d in D2:

cnt+=1

print(cnt)

print("\n\n=============================================\n\n")

cnt=0

for d in D3:

cnt+=1

print(cnt)

print("\n\n=============================================\n\n")

 

 

if __name__=="__main__":

main()

Explanation:

see code and see output attached

7 0
3 years ago
(in PYTHON) Write a function in REPL.it that uses three parameters, and squares the first, calculates mod 5 of the second parame
tatiyna

Answer:

I wrote this myself, it should be working. I think this is what the instructions were looking for.

The code below should return

Squared: 1296  

Mod: 0        

Quadrupled: 16

Explanation:

def threeParams(squared, mod, quadruples):

   array = [squared, mod, quadruples]

   array[0] = squared ** 2

   array[1] = mod % 5

   array[2] = quadruples * 4

   return array

valueArr = threeParams(36, 15, 4)

print(f"Squared: {valueArr[0]}\nMod: {valueArr[1]}\nQuadrupled: {valueArr[2]}")

4 0
2 years ago
What does spyware do on your computer?
Morgarella [4.7K]
It's a malware, and it basically let's the person/hacker/culprit get information off your computer without the owner of the computer knowing that the person is doing it. It's often used to find keystrokes, passwords, online interaction, and other personal data.
5 0
2 years ago
Explain synchronized plug firing.
eimsori [14]
HEY THERE!!

In a camera, flash synchronization is defined as synchronizing the firing of a photographic flash with the opening of the shutter admitting light to photographic film or electronic image sensor. It is often shortened to flash sync  or flash synch.


HOPE IT HELPS
6 0
2 years ago
Other questions:
  • Write a short paragraph explaining why cross-training employees is imperative.
    8·1 answer
  • This method is employed in order to rapidly change the locations of the website in order to ensure that no one site us used long
    13·1 answer
  • How long before a speech should you begin practicing?
    10·2 answers
  • How to block someone from watching your youtube videos?
    12·2 answers
  • Kaylen has been working on his computer and notices that the screen seems to be flickering. The monitor is not turning on and of
    13·1 answer
  • The communication channel used in IMC must rev: 12_06_2018_QC_ CDR-223 Multiple Choice match the traditional channel used in tha
    14·1 answer
  • Provide examples of the cost of quality based on your own experiences
    14·1 answer
  • I can't find the errors! Could anyone help me please?!
    5·1 answer
  • Attackers need a certain amount of information before launching their attack. One common place to find information is to go thro
    11·1 answer
  • Who addicted to fnaf
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!