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
#1
This is not homework. I was practicing python lists and wanted to total and average the simple list that I wrote. The code runs with no errors but it does not total or average at the end of the program. Should I have used a different type of loop when creating the list, like While instead of For? I like using the for statement as opposed to While when creating the list because it seems to be less code to write. Here's what I have.

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

#Procces list for total and average
total = 0
for acct in accountBalance:
    total = total + acct

average = total / len(accountBalance)
print("Total: ", total)
print("Average: ", round(average, 2))
Reply
#2
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: ')))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#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
#4
Have you seen this module: statistics
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  numbers in list - max value and average stewie 2 2,830 Apr-01-2020, 06:25 PM
Last Post: buran
  applying total in a loop to a list JustinxFFx 1 2,188 Feb-11-2018, 03:14 AM
Last Post: nilamo
  List showing running total of instances Pie_Fun 11 9,576 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