Python Forum
Python List with Total and Average
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python List with Total and Average
#3
(Nov-27-2018, 07:46 PM)ichabod801 Wrote: In for bal in accountBalance:, bal is independent of accoutBalance, so changing bal does not change accountBalance. So at the end, all the values are still 0. I would redo the first loop as:

accountBalance = []
for value in range(5):
    accountBalance.append(float(input('Enter value: ')))

I realized that the way I wrote it, I created the list and then created a separate loop to total the list. However, since the list was already created in the previous loop, there was nothing to total up. I rewrote it this way before seeing your comment, though yours would obviously work as well, this way worked just fine for the simple tasks I was asking. For more complicated lists and processing I think I should use your method.

#Create List with user input
accountBalance = [0] * 5
total = 0
for bal in accountBalance:
    bal = float(input("Enter"))
    total += bal

#Procces list for total and average


average = total / len(accountBalance)
print("Total: ", total)
print("Average: ", round(average, 2))
Reply


Messages In This Thread
Python List with Total and Average - by mcnhscc39 - Nov-27-2018, 07:13 PM
RE: Python List with Total and Average - by mcnhscc39 - Nov-27-2018, 08:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  numbers in list - max value and average stewie 2 2,892 Apr-01-2020, 06:25 PM
Last Post: buran
  applying total in a loop to a list JustinxFFx 1 2,273 Feb-11-2018, 03:14 AM
Last Post: nilamo
  List showing running total of instances Pie_Fun 11 9,761 Jan-20-2017, 11:29 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020