- Oct 31, 2012
- 8
- 0
- 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:
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.
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