Please I need help with this question. I have been trying to solve it, was able to solve a part, but not completely through with it
QUESTION:
Write a program that allows you to do the following five Operations:
a. Prompt the user to input a list of numbers (Hint: be sure to have a way for the user to indicate that they are done finished providing the list of numbers)
b. Open a file that contains a list of numbers
c. Compute the average, statistical median, and mode of the list of numbers
d. Store your answers in another file
e. Asks the user whether they want to see the answers and if the answer is yes, opens the file and displays the numbers
This is what I come up with:
# This program will ask for a user to input numbers. The numbers will then be calculated
# Open a file that contains a list of numbers.
# Compute the average, statistical mean, and median.
# Store your answers in another file
# Ask user whether they want to see the ansers and if the answer is yes, open the file and displays the numbers.
count = 0
sum = 0
number = 1
print 'Enter 0 to exit the list of numbers'
while number != 0:
number = input('Enter a number:')
count = count + 1
sum = sum + number
count = count - 1
variableMean = lambda x: sum(x)/len(x)
variableMedian = lambda x: x.sort() or x[len(x)//2]
variableMode = lambda x: max([x.count(y),y] for y in x)[1]
s = 'variableMean(number)'
y = 'variableMedian(number)'
t = 'variableMode(number)'
def variableMedian(x):
# don't modify original list
return sorted(x)[len(x)/2]
def variableMode(x):
# only iterate through x once (instead of len(x)times)
counts = {}
for items in x:
counts[x] = counts.get(x, 0) + 1
return max(count, key = counts._getitem_)
return sorted(count, key = counts._getitem_.reverse == True) [0]
x = (s, y, t)
f = ("avg.py")
import pickle
pickle.dump((s, y, t), file('avg.pickle', 'w'))
print 'Thank you! Would you like to see the results after calculating'
print 'The mode, median, and mean? (Please enter Yes or No)'
f = open("avg.py", "r")
data = f.read()
if raw_input == "Yes":
f = open("avg.py", "r")
data = f.read()
print 'The Mean average is:', sum/count
print 'The Median is:', variableMedian
print 'The Mode is:', variableMode
And the oputput I have after running the program is:
>>>
Enter 0 to exit the list of numbers
Enter a number:3
Enter a number:6
Enter a number:9
Enter a number:12
Enter a number:0
Thank you! Would you like to see the results after calculating
The mode, median, and mean? (Please enter Yes or No)
>>> 'Yes'
'Yes'
>>> variableMean = lambda x: sum(x)/len(x)
>>> variableMedian = lambda x: x.sort() or x[len(x)//2]
>>> variableMode = lambda x: max([x.count(y),y] for y in x)[1]
>>>
>>> s = 'variableMean(number)'
>>> y = 'variableMedian(number)'
>>> t = 'variableMode(number)'
>>>
>>> def variable_median(x):
# don't modify original list
return sorted(x)[len(x)/2]
>>> def variable_mode(x):
# only iterate through x once (instead of len(x)times)
counts = {}
for items in x:
counts[x] = counts.get(x, 0) + 1
return max(count, key = counts._getitem_)
return sorted(count, key = counts._getitem_.reverse == True) [0]
>>> x = (s, y, t)
>>> f = ("avg.py")
>>> import pickle
>>> pickle.dump((s, y, t), file('avg.pickle', 'w'))
>>>
>>> f = open("avg.py", "r")
>>> data = f.read()
>>>
>>>
KeyboardInterrupt
>>> print 'The Mean average is:', sum/count
The Mean average is: 7
>>> print 'The Median is:', variable_median
The Median is: <function variable_median at 0x00C429F0>
>>>
>>> print 'The Mode is:', mode
The Mode is:
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
print 'The Mode is:', mode
NameError: name 'mode' is not defined
>>> print 'The Mode is:', variablemode(number)
The Mode is:
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
print 'The Mode is:', variablemode(number)
NameError: name 'variablemode' is not defined
>>> print 'The Mode is:', variableMode(number)
The Mode is:
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
print 'The Mode is:', variableMode(number)
File "<pyshell#3>", line 1, in <lambda>
variableMode = lambda x: max([x.count(y),y] for y in x)[1]
TypeError: 'int' object is not iterable
>>> print 'The Mode is:', variable_mode
The Mode is: <function variable_mode at 0x00C42A30>
>>> print 'The Mode is:', variableMode
The Mode is: <function <lambda> at 0x00C42C30>
>>>
I have not been able to get the value for MEDIAN and MODE yet. Can anyone please help me out?
Thanks.