471,342 Members | 1,826 Online

# Help combining code 3
I have written some code that will find the average then the middle number and then the most used number of a user entered list of numbers. I have the code as separate programs and they all work fine separately. But I am trying to put them together as one. I have the average working fine but I can not get the middle and most to print. Also after the numbers have been entered I want it to save the list that is entered in to a file then recall it when prompted. I am completely lost. I am just learning this and I dont want the answer given to me I would like someone to look at my code and explain how I could do what I want. This is some of my code

if item == 'yes':
##f = open("amm.dat", "r")
print 'The Mean average is:', sum/count
print 'The Mode is:', () #this is here to give me a answer space
print 'The Median is:', m #same here
if item == 'no':
print 'Thank you Good bye'
# calculates mode
def mode(num):
if not num:
return None
num = {}
for item in num:
count = num.get(item, 0)
num[item] = count + 1
result = [ (count, item) for item, count in num.items() ]
result.sort()
result.reverse()
maxcount = result
modes = [ item for count, item in result if count == maxcount ]

# calculates median
def median(s):
i = len(s)
if i%2:
return (s[i/2]+s[(i/2)+1])/2.0
return s[i/2]
i = [i for i in (number) if i > median(number/2)]
Dec 2 '07 #1
4 1209 bvdet
2,851 Expert Mod 2GB
I have written some code that will find the average then the middle number and then the most used number of a user entered list of numbers. I have the code as separate programs and they all work fine separately. But I am trying to put them together as one. I have the average working fine but I can not get the middle and most to print. Also after the numbers have been entered I want it to save the list that is entered in to a file then recall it when prompted. I am completely lost. I am just learning this and I dont want the answer given to me I would like someone to look at my code and explain how I could do what I want. This is some of my code

Expand|Select|Wrap|Line Numbers
1.  = count + 1
2.      result = [ (count, item) for item, count in num.items() ]
3.      result.sort()
4.      result.reverse()
5.      maxcount = result
6.      modes = [ item for count, item in result if count == maxcount ]
7.
8. # calculates median
9. def median(s):
10.     i = len(s)
11.     if i%2:
12.         return (s[i/2]+s[(i/2)+1])/2.0
13.     return s[i/2]
14.     i = [i for i in (number) if i > median(number/2)]
Please use code tags.

I see one problem here:
Expand|Select|Wrap|Line Numbers
1. def mode(num):
2.      if not num:
3.          return None
4.      num = {}
5.      for item in num:
num is the argument. You are assigning num to an empty dictionary, then iterating on the empty dictionary. To print the individual results, use the string formatting operator:
Expand|Select|Wrap|Line Numbers
1. ....print 'The Mean average is: %.2f', thesum/count # float, 2 decimal places
2.     print 'The Mode is: %2f', mode(num)
3.     print 'The Median is: %2f', median(s)
You must return a calculation result in functions mode() and median() with the return statement. Do not use sum for a variable name because it will mask the built-in Python function sum().
Dec 2 '07 #2
bvdet
2,851 Expert Mod 2GB
To print the results and output to a file:
Expand|Select|Wrap|Line Numbers
1. f = open('output.txt', 'w')
2. output = 'The Mean average is: %.2f\nThe Mode is: %2f\nThe Median is: %2f' % \
3.          (sum/count, mode(num), median(s))
4. print output
5. f.write(output)
6. f.close()
Dec 2 '07 #3
elcron
43 I have written some code that will find the average then the middle number and then the most used number of a user entered list of numbers. I have the code as separate programs and they all work fine separately. But I am trying to put them together as one. I have the average working fine but I can not get the middle and most to print.
Expand|Select|Wrap|Line Numbers
1. >>> num = 8
2. >>> print num
3. 8
4. >>> print 'The Mode is:', num
5. The Mode is: 8
6. >>> print 'The Median is: %s'
7. The Median is: %s
Also after the numbers have been entered I want it to save the list that is entered in to a file then recall it when prompted.
Expand|Select|Wrap|Line Numbers
1. >>> myFile = file("MyTest.txt", "w")
2. >>> myFile.write("some Text.")
3. >>> myFile.write("some more Text.")
4. >>> myFile.close()
5. >>> myFile = file("MyTest.txt", "r")
7. 'some Text.some more Text.'
8.
9. >>> aList = [1,2,3,4,5] # for saving and loading as a string
10. >>> aList
11. [1, 2, 3, 4, 5]
12. >>> aStr = str(aList)
13. >>> aStr
14. '[1, 2, 3, 4, 5]'
15. >>> eval(aStr)
16. [1, 2, 3, 4, 5]
I am completely lost. I am just learning this and I dont want the answer given to me I would like someone to look at my code and explain how I could do what I want.This is some of my code
If you want to be able to combine and reuse code you should use functions and classes. Make sure you document any assumptions.
Expand|Select|Wrap|Line Numbers
1. # your function works fine and can be used in any code as long as your assumptions are met
2. def median(s):
3.     """
4.     median: works fine as long as  s is tuple or list of numbers sorted numbers
5.     """
6.     i = len(s)
7.     if i%2:
8.         return (s[i/2]+s[(i/2)+1])/2.0
9.     return s[i/2]
10.
Dec 2 '07 #4
calphord
3 thanks for the help i think i got it
Dec 3 '07 #5