473,378 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Help combining code

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[0][0]
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 1385
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[0][0]
  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")
  6. >>> myFile.read()
  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
thanks for the help i think i got it
Dec 3 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Unregistered | last post by:
Hello! I came across two different scripts that I wanted to combine, and I thought I wa successful until I discovered a minor glitch. What was happening was that the page that was linked to...
2
by: Chris Mullins | last post by:
I've spent a bit of time over the last year trying to implement RFC 3454 (Preparation of Internationalized Strings, aka 'StringPrep'). This RFC is also a dependency for RFC 3491...
7
by: Barry | last post by:
Hi all, I've noticed a strange error on my website. When I print a capital letter P with a dot above, using & #7766; it appears correctly, but when I use P& #0775 it doesn't. The following...
11
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field...
0
by: KK | last post by:
Hi, How to combine summary, detail reports into one report? I have a summary report summary.rpt, detail report detail.rpt I want to export these reports into a pdf file. I want to display...
5
by: ech0 | last post by:
I would appreciate any help I can get in finding a solution to the following problem: ================================================================== Below is a diagram:...
0
by: Jon | last post by:
All, I'm extremely new to XML and have been given a project that at this time seems way over my head. Was hoping someone here could help. Basically, I need a page that functions much like...
5
by: Tristan Miller | last post by:
Greetings. Is it possible using HTML and CSS to represent a combining diacritical mark in a different style from the letter it modifies? For example, say I want to render Å‘ (Latin small letter...
8
by: Sham | last post by:
I am trying to perform the following query on a table that has been indexed using Full Text Search. The table contains multiple columns than have been indexed. (Below, all xml columns are...
7
by: billelev | last post by:
I'm building a database and am a bit stumped about how to construct/link tables. I will describe the current configuration, then present the problem I am trying to solve. Currently: I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.