Results 1 to 6 of 6
  1. #1
    Matthew's Avatar
    Join Date
    Mar 2017
    Gender
    male
    Posts
    5,329
    Reputation
    1162
    Thanks
    1,156

    Python Program to Calculate Avg of Numbers in a Text File

    Coded in IDLE. Project was to create a program that read the contents of a .txt file and calculated the avg of all numbers in the file. This was to be done using 2 Higher - Order Functions (mapping and reducing) of your choice. I've test ints and decimals with this program and it has worked perfectly, have not tried putting strings into the test file but I'd assume it wouldn't work for obvious reasons.
    Code:
    import functools
    
    input_File = input('Name of file: ') #ask user for name of file
    file = open(input_File, 'r') #open file
    file = file.read() #read file
    
    file = file.split() #put numbers into list
    
    file = list(map(float, file)) #convert list into floats
    file_Sum = functools.reduce(lambda x, y: x + y, list(file)) #grabs sum of file 
    
    print(file_Sum / len(file)) #average function using file sum from reduce fuction

  2. The Following User Says Thank You to Matthew For This Useful Post:

    mburhm (07-02-2021)

  3. #2
    Bababam's Avatar
    Join Date
    Nov 2020
    Gender
    female
    Posts
    6
    Reputation
    10
    Thanks
    1
    Code:
    file = list(map(float, file)) #convert list into floats
    You can to implement a Try/Except block inside your map function at the point it does the data type conversion. If it throws an exception then just return null or 0 and suppress the error message.

  4. The Following User Says Thank You to Bababam For This Useful Post:

    Matthew (02-08-2021)

  5. #3
    Future's Avatar
    Join Date
    Oct 2015
    Gender
    male
    Location
    Skies of blue
    Posts
    1,299
    Reputation
    428
    Thanks
    14,335
    Code:
    sum = 0
    input_File = input('Name of file: ')
    file = open(input_File, 'r') 
    file = file.read()
    
    file = file.split()
    total = len(file)
    
    for num in file:
         sum += int(num)
    
    print (f"Average number in {file} is {sum/total}")
    MPGH's Rules | Marketplace | How IM works | Report Scam Attempt
    In case you need me : IM | PM

    Middleman from 29/01/2021 - present
    Minion+ from 12/04/2020 - present.
    Minion from 09/01/2020 - 12/04/2020.
    MPGH News Force from 02/11/2019 - not sure anymore.
    Resource Team from 02/11/2019 - 12/04/2020.
    Premium Member since 16/09/2019.
    Member since 13/10/2015.


  6. #4
    egycnq's Avatar
    Join Date
    Sep 2018
    Gender
    male
    Posts
    198
    Reputation
    84
    Thanks
    34
    My Mood
    Amazed
    good project

  7. #5
    Someone123456's Avatar
    Join Date
    Sep 2011
    Gender
    male
    Posts
    131
    Reputation
    10
    Thanks
    11
    My Mood
    Tired
    Yo I like this ,very nice !

  8. #6
    mburhm's Avatar
    Join Date
    Jul 2021
    Gender
    male
    Posts
    1
    Reputation
    10
    Thanks
    0

    Thumbs up good project

    good project

Similar Threads

  1. [Source Code] Python Program to Calculate Weekly Pay
    By Matthew in forum Python Programming
    Replies: 0
    Last Post: 09-26-2020, 07:19 PM
  2. [Source Code] Python Program to Calculate Sum/Avg of a Given Input
    By Matthew in forum Python Programming
    Replies: 0
    Last Post: 09-26-2020, 07:17 PM
  3. Python Programming Homework (University, Intermediate level)
    By TallOne123 in forum Work & Job Offers
    Replies: 0
    Last Post: 02-04-2016, 05:33 PM
  4. Paying for a short text based python program
    By Braddy12 in forum Coders Lounge
    Replies: 1
    Last Post: 05-09-2015, 11:17 AM
  5. [Help] Hacking Python programs
    By boynedmaster in forum Coders Lounge
    Replies: 0
    Last Post: 03-25-2015, 06:03 PM