Averaging a list of numbers the user inputs [Python]
Question Description
Ok so this is what I'm being asked to do:
Write a program that asks the user to enter some numbers (positives, negatives and zeros). Your program should NOT ask the user to enter a fixed number of numbers. Also it should NOT ask for the number of numbers the user wants to enter. But rather it should ask the user to enter a few numbers and end with -9999 (a sentinel value). The user can enter the numbers in any order. Your program should NOT ask the user to enter the positive and the negative numbers separately. Your program then should create a list with the numbers entered (make sure NOT to include the sentinel value (-9999) in this list) and output the list of numbers and another list with the following averages (using the input list and the above functions):
[the average of all the positive numbers, the average of all the non-positive numbers, the average of all the numbers]
Sample run:
Enter a number (-9999 to end): 4 Enter a number (-9999 to end): -3 Enter a number (-9999 to end): -15 Enter a number (-9999 to end): 0 Enter a number (-9999 to end): 10
Enter a number (-9999 to end): 22 Enter a number (-9999 to end): -9999 The list of all numbers entered is: [4, -3, -15, 0, 10, 22] The list with averages is: [12.0, -6.0, 3.0]
And this is my code so far:
def nums():
values = []
while True:
x = int(input("Enter any amount of numbers or -9999 to quit: "))
if x == -9999:
break
values.append(x)
print values
return values
def allNumAvg(values):
average = 0
sum = 0
for n in values:
sum = sum + n
average = sum / len(values)
return average
def posNumAvg(values):
x = []
average = 0
for i in values:
if i > 0:
x.append(i)
average = sum(x) / len(x)
return average
def negativeNumAvg(values):
x = []
average = 0
for i in values:
if i < 0:
x.append(i)
average = sum(x) / len(x)
return average
nums()
print mylist
allNumAvg(values)
Everything is indendted correctly in my program, idk why it's messing up here. But when I run it only gets to the nums() part, it doesnt find the averages or anything and I have no idea why
This question has not been answered.
Create a free account to get help with this and any other question!
Brown University
1271 Tutors
California Institute of Technology
2131 Tutors
Carnegie Mellon University
982 Tutors
Columbia University
1256 Tutors
Dartmouth University
2113 Tutors
Emory University
2279 Tutors
Harvard University
599 Tutors
Massachusetts Institute of Technology
2319 Tutors
New York University
1645 Tutors
Notre Dam University
1911 Tutors
Oklahoma University
2122 Tutors
Pennsylvania State University
932 Tutors
Princeton University
1211 Tutors
Stanford University
983 Tutors
University of California
1282 Tutors
Oxford University
123 Tutors
Yale University
2325 Tutors