I am trying to write two functions. One generates random numbers between 1 and 100 with the parameter being the frequency of numbers which saves the information to a file, and another one reads this file and displays the: total sum, average, highest and lowest value. I have managed to get the first function to work however the secound function does not. When running the program I get the TypeError: 'int' object is not iterable due to the "number=int(number)" line in the readNumbers() function, which I don't understand because I thought the number had to be changed from a string to an int in order for equations to work. Any help would be appreciated. Also is there a method of finding the maximum and minimum values without using max() and min(), I would personally like to know.
def generateNumbers(i):
myFile=open("numbers.txt","w")
for n in range(i):
import random
numbers=random.randint(1,100)
numbers=str(numbers)
myFile.write(numbers)
myFile.close()
generateNumbers(400)
def readNumbers():
myFile=open("numbers.txt","r")
for number in myFile:
number=int(number)
total=(sum(number))
average=sum/float(len(number))
while number!=-1:
num_list.append(number)
high= max(num_list)
low= min(num_list)
print(total,average,high,low)
myFile.close()
readNumbers()
Your program has several problems.
Writing the numbers:
You're not separating the numbers in anyway, so your file will contain a very long string of digits (and to nitpick, your numbers is actually random_number)
if you f.ex myFile.write("\n") after myFile.write(random_number) each number
will be separated by a newline.
Now, since separating the numbers with newline, your reading will work, except that you should read them into an array, then do total and average.
ie:
num_list = []
for number in myFile:
if number != "\n": # there is an empty line at the end
num_list.append(int(number))
total = sum(num_list)
average = total / len(num_list)
high = max(num_list)
low = min(num_list)
Note that you don't need the while loop
You could also do the reading with python's list comprehension, and close the file automatically with a context manager
with open("numbers.txt", "r") as f:
num_list = [int(number) for number in f if number != "\n"]
(edited to avoid error on empty line at end)
Kindal is right.
You should do something like:
sum = 0;
count = 0;
for number in myFile:
sum += number
count += 1
average = sum /count
Moreover, I think that you can optimise a bit your code. Read the file and create a list, then you can easily calculate sum, avg, min, max.
with open("my filename.txt", 'r') as myFile:
number_list = list()
for number in myFile:
number = int(number)
if number != -1: #Is it necessary for you?
number_list.append(number)
sum = sum(number_list)
average = sum / len(number_list)
min_val = min(number_list)
max_val = max(number_list)
Related
I need to read an input file (input.txt) which contains one line of integers (13 34 14 53 56 76) and then compute the sum of the squares of each number.
This is my code:
# define main program function
def main():
print("\nThis is the last function: sum_of_squares")
print("Please include the path if the input file is not in the root directory")
fname = input("Please enter a filename : ")
sum_of_squares(fname)
def sum_of_squares(fname):
infile = open(fname, 'r')
sum2 = 0
for items in infile.readlines():
items = int(items)
sum2 += items**2
print("The sum of the squares is:", sum2)
infile.close()
# execute main program function
main()
If each number is on its own line, it works fine.
But, I can't figure out how to do it when all the numbers are on one line separated by a space. In that case, I receive the error: ValueError: invalid literal for int() with base 10: '13 34 14 53 56 76'
You can use file.read() to get a string and then use str.split to split by whitespace.
You'll need to convert each number from a string to an int first and then use the built in sum function to calculate the sum.
As an aside, you should use the with statement to open and close your file for you:
def sum_of_squares(fname):
with open(fname, 'r') as myFile: # This closes the file for you when you are done
contents = myFile.read()
sumOfSquares = sum(int(i)**2 for i in contents.split())
print("The sum of the squares is: ", sumOfSquares)
Output:
The sum of the squares is: 13242
You are trying to turn a string with spaces in it, into an integer.
What you want to do is use the split method (here, it would be items.split(' '), that will return a list of strings, containing numbers, without any space this time. You will then iterate through this list, convert each element to an int as you are already trying to do.
I believe you will find what to do next. :)
Here is a short code example, with more pythonic methods to achieve what you are trying to do.
# The `with` statement is the proper way to open a file.
# It opens the file, and closes it accordingly when you leave it.
with open('foo.txt', 'r') as file:
# You can directly iterate your lines through the file.
for line in file:
# You want a new sum number for each line.
sum_2 = 0
# Creating your list of numbers from your string.
lineNumbers = line.split(' ')
for number in lineNumbers:
# Casting EACH number that is still a string to an integer...
sum_2 += int(number) ** 2
print 'For this line, the sum of the squares is {}.'.format(sum_2)
You could try splitting your items on space using the split() function.
From the doc: For example, ' 1 2 3 '.split() returns ['1', '2', '3'].
def sum_of_squares(fname):
infile = open(fname, 'r')
sum2 = 0
for items in infile.readlines():
sum2 = sum(int(i)**2 for i in items.split())
print("The sum of the squares is:", sum2)
infile.close()
Just keep it really simple, no need for anything complicated. Here is a commented step by step solution:
def sum_of_squares(filename):
# create a summing variable
sum_squares = 0
# open file
with open(filename) as file:
# loop over each line in file
for line in file.readlines():
# create a list of strings splitted by whitespace
numbers = line.split()
# loop over potential numbers
for number in numbers:
# check if string is a number
if number.isdigit():
# add square to accumulated sum
sum_squares += int(number) ** 2
# when we reach here, we're done, and exit the function
return sum_squares
print("The sum of the squares is:", sum_of_squares("numbers.txt"))
Which outputs:
The sum of the squares is: 13242
I have a simple script that takes a list containing 3 columns of data. The second column of data contains currency values with a leading dollar sign. I have stripped away the dollar sign from the second column, now I need to add up the values. I'm getting a "decimal.Decimal is not iterable" error. Here is the code:
from decimal import Decimal
def main():
total = 0.0
try:
infile = open('list.txt', 'r')
for i in infile:
parts = i.split()
if len(parts) > 1:
dollar_dec = Decimal((parts[1]).strip('$'))
total = sum(dollar_dec)
print (total)
infile.close()
except Exception as err:
print(err)
main()
Say, you have the following file content:
content = """\
one $1.50
two $3.00
three $4.50"""
You can use the in-place operator += to calculate the total:
from decimal import Decimal
import io
total = Decimal(0)
with io.StringIO(content) as fd:
for line in fd:
parts = line.strip().split()
if len(parts) > 1:
dollard_dec = Decimal(parts[1].strip("$"))
total += dollard_dec
print(total)
Her, you get: 9.00
You can also use sum() on a iterable (here a comprehension list):
with io.StringIO(content) as fd:
total = sum(Decimal(line.strip().split()[1].strip("$"))
for line in fd)
print(total)
Yes, you get 9.00 too!
total = sum(dollar_dec)
sum() takes an iterable (a list, for example) and adds up all the values. You are passing it a single number, which is an error. You probably want
total = Decimal('0.0')
...
total += dollar_dec
Which will keep a running total.
(edit- total must be a Decimal for you to add Decimals to it)
sum() takes an iterable. Just change your code to total += dollar_dec
fname = input("Enter file name: ")
count=0
fh = open(fname)
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
count=count+1
halo=line.find("0")
gh=line[halo:]
tg=gh.rstrip()
ha=float(tg)
total=0
for value in range(ha):
total=total+value
print total
its like a list of decimal number in file ok
0.1235
0.1236
0.1678
I convert it into float where 'tg' have not an array like a list
ha=float(tg)
total=0
for value in range(ha):
total=total+value
print total
error: start must be an integer
I know it's a mistake of using range what should I use instead of range?
If you want to get a sum of floats, just use the code:
fname = input("Enter file name: ")
count = 0
total = 0
fh = open(fname)
for line in fh:
if not line.startswith("X-DSPAM-Confidence:"): continue
count += 1
halo = line.find("0")
gh = line[halo:]
tg = gh.rstrip()
ha = float(tg)
total += ha
print total
You are passing a float as argument to range, which does not make sense. range returns a list with n elements when n is the only argument of range. For example:
>>> range(3)
[0, 1, 2]
So you can see that range of a float does not make sense.
If I understand your code correctly, I think you want to replace:
for value in range(ha):
total=total+value
By
total += ha
On a separate note, and trying not to be too pedantic, I am pretty impressed by how many principles of PEP 8 your code violates. You may think it's not a big deal, but if you care, I would suggest you read it (https://www.python.org/dev/peps/pep-0008/)
So far, my code finds the largest number in a text file but it doesn't find the smallest number, when it's run the smallest number is still 0 while the largest is 9997.
integers = open('numbers.txt', 'r') # opens numbers.txt
largestInt = 0 # making variables to store the largest/smallest int
smallestInt = 0
# loop where we check every line for largest/smallest int
for line in integers:
while largestInt <= line.strip():
largestInt = line
while smallestInt >= line.strip():
smallestInt = line
# print results
print "Smallest = ", smallestInt
print "Largest = ", largestInt
numbers.txt looks like:
6037
-2228
8712
-5951
9485
8354
1467
8089
559
9439
-4274
9278
-813
1156
-7528
1843
-9329
574
and so on.
What's wrong here? If I'm doing something wrong, or the logic is incorrect please correct me.
EDIT
I'd like to say thanks to #Martijn Pieters and #Gexos for for explaining what I'm doing wrong. I understand why my code works now!
Final Code:
integers = open('numbers.txt', 'r') # opens numbers.txt
largestInt = 0 # making variables to store the largest/smallest int
smallestInt = 0
# loop where we check every line for largest/smallest int
for line in integers:
if largestInt <= int(line.strip()): # converted a string into an int
largestInt = int(line.strip()) # made that int into largestInt
if smallestInt >= int(line.strip()): # converted a string into an int
smallestInt = int(line.strip()) # made that int into smallestInt
integers.close() # closes the file
# print results
print "Smallest = ", smallestInt
print "Largest = ", largestInt
Results
Smallest = -9993
Largest = 9997
You are comparing strings, not integers; turn your line into an integer before comparing:
largestInt = float('-inf')
smallestInt = float('inf')
for line in integers:
number = int(line)
if largestInt < number:
largestInt = number
if smallestInt > number:
smallestInt = number
Note that you want to use if here, not while; the latter creates a loop.
I started largestInt and smallestInt with float('-inf') and float('inf'), respectively, numbers guaranteed to be smaller and larger than anything else. This makes the first time you test for largestInt < number always true, whatever number is on the first line.
Comparing strings is done lexicographically, where characters are compared one by one; 10 is smaller than 2 because 1 sorts before 2.
You could use the max() and min() built-in functions for ease, but it'll be a bit less efficient because internally these functions do loops as well:
numbers = {int(line) for line in integers}
largestInt = max(numbers)
smallestInt = min(numbers)
While I usually like solutions that use min and max, that would require two linear passes in this case, with a lot of memory overhead. Here's a method that needs one linear pass and constant memory:
with open('numbers.txt') as infile:
smallest, largest = '', None
for line in infile:
n = int(line)
smallest = min(n, smallest)
largest = max(n, largest)
print "the smallest number is", smallest
print "the largest number is", largest
As others have stated, you need to convert your lines to integers first. Additionally, your script will not output the correct minimum number if that number is bigger than zero. To fix this, set both your maximum number and minimum number to the first entry in your file. Then check all other numbers, and see if they're bigger/smaller than the current number.
with open('numbers.txt', 'r') as data_file:
num = int(next(data_file))
min_num, max_num = num, num
for line in data_file:
num = int(line)
if num > max_num:
max_num = num
elif num < min_num:
min_num = num
print 'Smallest number: {}'.format(min_num)
print 'Largest number: {}'.format(max_num)
This can also be solved with list comprehensions:
nums = [int(line) for line in open('numbers.txt', 'r')]
min_num, max_num = min(nums), max(nums)
A few things. You need to cast the line into an int from a string: int(line.strip()) currently you are comparing an int to a string. You should also cast the assignment in largestInt = int(line.strip()) and the same for smallestInt.
You should not be using while. while is for looping, not for comparing. You should be using if.
And last but not least make sure to close the file at the end. integers.close()
integers = open('numbers.txt', 'r')
intList = [int(x) for x in integers.readlines()]
print max(intList), min(intList)
with open('number.txt') as f:
s = [int(n.strip()) for n in f]
print min(s), max(s)
You are comparing strings, not ints. You need to call the int function on them at some stage most likely to convert them to numbers.
This is an example of a different approach:
with open('numbers.txt', 'r') as f:
integers = [int(n) for n in f.readlines()]
smallest = min(integers)
biggest = max(integers)
using with ensures the file is auto closed after the list comprehension, which is good practice. The list comprehension results in:
[6037, -2228, 8712, -5951, 9485, 8354, 1467, 8089, 559, 9439, -4274, 9278, -813, 1156, -7528, 1843, -9329, 574]
Then min and max are called on that list.
One thing to note when setting your smallestInt to 0. If the smallest number your text document contains is 1 then you'll still end up with a 0 as an answer.
in my data file I have
60,66,88,90,44,90,80,77
all the numbers are in one line
this is my code which does not give me the average of my numbers
inFile3 = open("data2.txt","r")
global gradeVar
gradeVar = len(gradeArray)
global Total
Total = 0
for Numbers in inFile3:
Total = Total + int(Numbers)
inFile3.close()
global averageVar
averageVar = Total/gradeVar
return averageVar
This is the error
Traceback (most recent call last):
File "program.py", line 81, in <module>
main()
File "program.py", line 5, in main
averageVar = Average()
File "program.py", line 39, in Average
Total = Total + int(Numbers)
ValueError: invalid literal for int() with base 10: '60,66,88,90,44,90,80,77\n'
Your problem is here:
for Numbers in inFile3:
Total = Total + int(Numbers)
Numbers in the code above is a list of lines, rather than a list of numbers.
for Line in inFile3:
for number in Line.split(','):
Total = Total + int(number)
should help.
You also have no need to pre-declare variables the way you are in Python. In fact, doing so with global is positively dangerous unless you know what you're doing and why.
Edit:
If you ever have a comma at the end of a line, of a blank value you can change the final line to:
if number.strip():
Total = Total + int(number)
This will ignore any 'empty' number strings that would otherwise throw an error.
This line:
for Numbers in inFile3:
is actually iterating over the lines of the file, not the numbers within each line. You need to iterate over the lines, then for each line split it into numbers, something like this:
for Line in inFile3:
for Number in Line.split(','):
Total = Total + int(Number)
While others have pointed out some of the issues involved in what you are doing, none have pointed out that an average needs not only the sum of all the parts but also the number of all the elements. Hence,
def parseNumberFile(file_name):
for numbers in open(file_name, "r"):
items = numbers.split(',')
yield (len(items), sum(map(int,items)))
which turns it into a generator that you can use as:
total = 0
count = 0
for x,y in parseNumberFile("myData.txt"):
count += x
total += y
average = total/count
Where are you reading the data? You'll need to read it in and then split the String into numbers with something like str.split().
Here is a more pythonic way:
inFile3 = open("data2.txt","r")
grade_list = inFile3.readline()
inFile3.close()
num_list = [int(g) for g in grade_list.split(',')]
average = sum(num_list) / len(num_list)
print average
The error message says it all: '60,66,88,90,44,90,80,77\n', considered as a group, is not a valid integer. You need to consider them one by one. First, strip off the new line, then split by the comma.
Change:
for Numbers in inFile3:
To:
# assumes numbers are all on one line with no spaces between
for Numbers in inFile3.read().strip().split(','):
If I had to rewrite from scratch:
from __future__ import division # this import is not needed in python 3
with open('data2.txt', 'r') as f:
numbers = [int(n) for n in f.read().strip().split(',')]
avg = sum(numbers) / len(numbers)
Change
for Numbers in inFile3:
to
for Numbers in inFile3.strip().split(','):