As the title states, I’m trying to create (def) a function wherein I import re and then parse through a string (using Regular Expressions) to add the numbers up at the end in a list. I know the code works outside of the function I am trying to save. I’ve just never created a function this complex so I’m unsure of indentation usage or if it’s possible.
#create function to import the re lib and then parse through "fhand" after the file is opened by calling this function.
def Ptfs():
import re
count = 0
total = list()
for line in fhand:
numbers = re.findall('[0-9]+' ,line)
for number in numbers :
num = int(number)
total.append(num)
count = count + 1
print(sum(total), "There were", count, "lines that were summed.")
fhand = open("file_name", 'r')
Ptfs()
New contributor
Naethan Peters is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.