Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8475089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:48:03+00:00 2026-06-10T17:48:03+00:00

I’m still new at this but I think I am close. My assignment is

  • 0

I’m still new at this but I think I am close. My assignment is to calculate the mean, median, and mode, then:

  • 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)
  • Open a file that contains a list of numbers
  • Compute the average, statistical median, and mode of the list of numbers
  • Store your answers in another file
  • Asks the user whether they want to see the answers and if the answer is yes, opens the file and displays the numbers

I would like to have the file more “readable”. an example of the result I get now is:

(lp1
S'3,5,6'
p2
aF4.666666666666667
aF5
aF3
a.

Any help would be greatly appreciated!

Here is my code:

def mean(numbers):
    average = 0
    for number in numbers:
        average += number
    return average / float(len(numbers))

def median(numbers):
    middle = []
    for number in numbers:
        middle.append(number)
    middle.sort()
    middleValue = len(middle) / 2
    if len(middle) % 2 == 1:
        return middle[middleValue]
    else:
        return (middle[middleValue] + middle[middleValue - 1]) / 2

def mode(numbers):
    frequency = {}
    for number in numbers:
        times = frequency.get(number, None)
        if times == None:
            frequency[number] = 1
        else:
            frequency[number] = times + 1

    maxValue = max(frequency.values())
    for modeFreq in frequency:
        if frequency[modeFreq] == maxValue:
            return modeFreq        

import cPickle as pickle

def main():
    user_in = raw_input("Enter a list of numbers seperated by commas: ")
    numbers_as_strings = user_in.replace(' ', '').split(',')
    numbers = [float(x) for x in numbers_as_strings]
    if not user_in:
        return 0
    else:
        print "\nYour inputs: ", user_in, '\n'
        print "   Mean = ", mean(numbers)
        print " Median = ", median(numbers)
        print "   Mode = ", mode(numbers), '\n'
        pckl_file = file("mean_medi_mode.txt", "w")
        pickle.dump([user_in, mean(numbers),median(numbers),mode(numbers)],pckl_file)
main()

while True:
    answer = raw_input('Would you like to open a file with your results? ')
    if answer.strip() in "y Y yes Yes YES".split():
        f = open("mean_medi_mode.txt", 'r')
        for line in f:
            print line,
        print 'Done'
        break
    elif answer.strip() in "n N no No NO".split():
        print 'Done'
        break
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:48:04+00:00Added an answer on June 10, 2026 at 5:48 pm

    Try to use the json module. It’s easy to use and human-readable

    import json
    code...
    def main():
        user_in = raw_input("Enter a list of numbers seperated by commas: ")
        numbers_as_strings = user_in.replace(' ', '').split(',')
        numbers = [float(x) for x in numbers_as_strings]
        if not user_in:
            return 0
        else:
            print "\nYour inputs: ", user_in, '\n'
            print "   Mean = ", mean(numbers)
            print " Median = ", median(numbers)
            print "   Mode = ", mode(numbers), '\n'
            json.dump([user_in, mean(numbers), median(numbers), mode(numbers)], fp)
    

    Then:

    def print(fp):
        user_in, mean, median, mode = json.load(fp)
        print "\nYour inputs: ", user_in, '\n'
        print "   Mean = ", mean
        print " Median = ", median
        print "   Mode = ", mode, '\n'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.