443,813 Members | 1,130 Online Need help? Post your question and get tips & solutions from a community of 443,813 IT Pros & Developers. It's quick & easy.

# Help combining code

 P: 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 Replies

 Expert Mod 2.5K+ P: 2,851 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  = 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)] Please use code tags. I see one problem here: Expand|Select|Wrap|Line Numbers def mode(num):      if not num:          return None      num = {}      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 ....print 'The Mean average is: %.2f', thesum/count # float, 2 decimal places     print 'The Mode is: %2f', mode(num)     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

 Expert Mod 2.5K+ P: 2,851 To print the results and output to a file: Expand|Select|Wrap|Line Numbers f = open('output.txt', 'w') output = 'The Mean average is: %.2f\nThe Mode is: %2f\nThe Median is: %2f' % \          (sum/count, mode(num), median(s)) print output f.write(output) f.close() Dec 2 '07 #3

 P: 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 >>> num = 8 >>> print num 8 >>> print 'The Mode is:', num The Mode is: 8 >>> print 'The Median is: %s' 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 >>> myFile = file("MyTest.txt", "w") >>> myFile.write("some Text.") >>> myFile.write("some more Text.") >>> myFile.close() >>> myFile = file("MyTest.txt", "r") >>> myFile.read() 'some Text.some more Text.'   >>> aList = [1,2,3,4,5] # for saving and loading as a string >>> aList [1, 2, 3, 4, 5] >>> aStr = str(aList) >>> aStr '[1, 2, 3, 4, 5]' >>> eval(aStr) [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 # your function works fine and can be used in any code as long as your assumptions are met def median(s):     """     median: works fine as long as  s is tuple or list of numbers sorted numbers     """     i = len(s)     if i%2:         return (s[i/2]+s[(i/2)+1])/2.0     return s[i/2]    Dec 2 '07 #4

 P: 3 thanks for the help i think i got it Dec 3 '07 #5 