Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File Reading
#1
This assignment is technically complete according to my instructions, but I would like to polish it up a bit for my own learning experience. Basically, I was tasked to display 15 numbers in a file, then calculate the average of said numbers.

# Initialize constants
total = 0
lines = 0
file = 0
average = 0

# Gather all numbers in file for display
with open('numbers.dat', 'r') as file:
    lines = file.read()

# Add all numbers in file
for add in open('numbers.dat'):
    total += int(add.strip())

# Calculate the average of numbers in file
average = total / 15
average = round(average, 2)

#Print all numbers in file
print('The numbers in the file are:')
print(lines)

# Print the average of numbers in file
print('The average of those numbers is:')
print(average)

# Close file
file.close()

# END #
Question 1:
In this line of code:
# Calculate the average of numbers in file
average = total / 15
average = round(average, 2)
How can I replace the "15" with the number of integers that are actually in the file? Say I wanted to put 20 numbers in there instead of 15 to calculate the average, I'd like the total to be divided by the amount of numbers there are, not just a constant.

Question 2: What can I add to ignore spaces in the file, and only use the numbers on each line? The code works fine if the numbers are in a nice column without any spaces; however, I get an error if there is a single space in between the numbers.

Please keep any suggestions as close to the initial code as possible, and thank you for reading this far.

Cheers.
Reply


Messages In This Thread
File Reading - by toxicxarrow - May-07-2018, 09:40 AM
RE: File Reading - by ThiefOfTime - May-07-2018, 10:14 AM
RE: File Reading - by wavic - May-07-2018, 10:30 AM
RE: File Reading - by killerrex - May-07-2018, 10:35 AM
RE: File Reading - by toxicxarrow - May-07-2018, 10:57 AM
RE: File Reading - by killerrex - May-07-2018, 01:47 PM
RE: File Reading - by wavic - May-07-2018, 02:02 PM
RE: File Reading - by toxicxarrow - May-07-2018, 03:02 PM
RE: File Reading - by wavic - May-07-2018, 03:08 PM
RE: File Reading - by toxicxarrow - May-07-2018, 04:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Excel File reading vanjoe198 1 2,068 Mar-31-2021, 11:53 AM
Last Post: snippsat
  reading from a file looseCannon101 14 5,114 Jul-18-2020, 11:29 AM
Last Post: GOTO10
  Weird problem with reading from file and performing calculations pineapple999 1 3,047 Jul-25-2019, 01:30 AM
Last Post: ichabod801
  Handling IO Error / Reading from file Expel 10 4,989 Jul-18-2019, 01:21 PM
Last Post: snippsat
  Reading an Unconventional CSV file OzSbk 2 3,923 May-17-2019, 12:15 PM
Last Post: MvGulik
  reading text file and writing to an output file precedded by line numbers kannan 7 10,529 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Reading of structured .mat (matlab) file sumit 2 3,474 May-24-2018, 12:12 PM
Last Post: sumit
  reading all lines from a text file seadoofanatic 2 2,962 Mar-13-2018, 06:05 PM
Last Post: Narsimhachary
  Reading a text file fivestar 7 5,689 Oct-13-2017, 07:25 AM
Last Post: gruntfutuk
  Dictionary + File Reading palmtrees 2 4,931 Nov-15-2016, 05:16 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020