I have to calculate the sum of any numbers in the file and print the sum.
A number is defined as any string beginning with a digit 0 through 9 followed by any numbe
All you need to do is use item.isnumeric()
. If the item made up of only numbers and not letters or other characters it will return true.
So you check the all the items in wordList
and if the item isnumeric()
you add the item to total
.
infile = open(filename.txt, 'r')
content = infile.read()
infile.close()
wordList = content.split()
total = 0
for item in wordList:
if item.isnumeric():
total += int(item)