# Python help- lists calculations

Discussion in 'Programming' started by lemonhead71, Nov 14, 2012.

Joined:
Oct 31, 2012
Messages:
8
0
Hi, I'm working on question 2 on http://bohr.wlu.ca/cp104/assignments/asgn16.php?d=1353042000 and I'm just stumped. I made a function that receives inputs from the user and forms the appropriate list as shown in the question:
Code:
```def weighted_average():
list_number=[]
total_denominator=0
user_continue='Y'
while user_continue=='Y' or user_continue=='y':
m=int(input('Enter the number of the same items: ').strip())
x=float(input('Enter the price of the item: ').strip())
user_continue=str(input('Would you enter a different item? Enter "Y" for yes and "N" for no: ' ).strip())
mx=[m,x]
list_number.append(mx)

return  ```
But I' having difficulty in actually doing the weighted average part. If the list was [[2,0.36],[3,1.25]] I know the calculation would be (list_number[0][0]*list_number[0][1]+list_number[1][0]*list_number[1][1])/(list_number[0][0]+list_number[1][0]), but since the number of entries in the list is unknown and up to the user I'm not sure how to code for the weighted average. Thank you for your time.

#1

2. Similar Threads - Python lists calculations Forum Date
basic python 2.7 print method question Programming May 8, 2017
Should I look into learning Python? Programming Apr 24, 2017
Python syntax compile error Programming Mar 30, 2017
python/recreating checksum value Programming Feb 27, 2017

3. ### mv2devnull Golden Member

Joined:
Apr 13, 2010
Messages:
1,025
14
Iterate over the list and sum up the values that has to be summed up.

#2

Joined:
Oct 31, 2012
Messages:
8
0
My problem is i don't know exactly how to sum up the values that has to be summed up. I know I should use a for loop for this but I'm not sure how to get the for loop to iterate in such a way that: list_number[0][0]*list_number[0][1]+list_number[1][0]*list_number[1][1]....list_number[n][0]*list_number[n][1], where n is an integer.

#3
5. ### mv2devnull Golden Member

Joined:
Apr 13, 2010
Messages:
1,025
14
Surely Python has a loop syntax that steps an integer i from 0 to n.

#4
6. ### Cerb Elite Member

Joined:
Aug 26, 2000
Messages:
17,409
1
Yes, it does, but not so directly as some languages: you even said it and didn't know it.

It's, "one those things," with Python. 'For' iterates, so to step through a range of integers, you use an iterator that generates those integers, in order.

If the language is going to be slow, it should be able to do fancy stuff, to make that worthwhile.

Code:
```# Python 2:
# range returns a list. Big lists can cause problems.
# xrange returns an iterator, so won't chew up so much RAM.
for i in xrange( 0, n+1 ):
...

# Python 3:
for i in range( 0, n+1 ):
...

# If stuck in Python 3, and needing to generate a list...
# because no matter how many times others say you
# shouldn't do it that way, sometimes it's the best
# way to make what you need with very little code:
my_list = list( range( begin, end[, step ]) )```
However, for the OP's problem, the right way to do it is to iterate over an actual list, as given in the problem description.

Use an over variable and an under variable. Update them each iteration through the loop. After the loop, divide for the result.

#5
7. ### degibson Golden Member

Joined:
Mar 21, 2008
Messages:
1,389
```x = ["Hello ", "World!\n", "Iterate ", "like ", "this."]