i have: def total_money(list): total = 0 if list == []: return 0 for (x,y) in li
ID: 3596981 • Letter: I
Question
i have:
def total_money(list):
total = 0
if list == []:
return 0
for (x,y) in list:
total = y + total
return total
what am i doing wrong?
Use the Design Recipe to write a function called total money, that consumes a list of students, where each student is represented as a tuple where the first item is their name(string), and the second item is how much money they have (float). This function should return a total of all the student's money For example: Test students print(total_money (students)) students [("HattSap" , 100000000)] print(total _money (students)) students = [("Nessy", 3.5), ("Curtis James Jackson", print (total _money (students)) Result 100000000 o.5)] | 4.0Explanation / Answer
In python indentation must and should you have written exactly below the "total = y + total" which makes python to understand that it belongs to for loop. So in the first loop itself it returns the first value of the money without adding all the values.
So in this case you have to write the return parllel to for loop, so that python treats return is after the for loop.
So the code must be :
---------------------------------------------------------------------------------------------
def total_money ( list ):
total = 0
if( list == [ ] )
return 0
for (x,y) in list :
total = y + total
return total
------------------------------------------------------------------------------------------
In this way you should take care of indentation while writing python scripts. Python decides the program blocks based on the indentation.
/* hope this helps */
/* if any queries, please comment */
/* thank you */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.