The program must use a list of length 10 to count the occurrences of the 10 digits 0-9.
It should print like this:
I am not sure how to get it to add up the occurrences of each number every time it reads a line. Or how to get the totals into a list.
Number of 0's: 5
Number of 1's: 8
Number of 2's: 17
def main():
intro()
inFile = getFile()
file, outfile = convertName(inFile)
count, counts = countLines(file, outfile)
printResults(count, counts)
def intro():
print()
print("Program to count letters in each line")
print("in a file.")
print("You will enter the name of a file.")
print("The program will create an output file.")
print("Written by --------.")
print()
def getFile():
inFile = input("Enter the name of input file: ")
return inFile
def convertName(inFile):
file = open(inFile, "r")
outfile = (inFile.replace(".txt", ".out"))
return file, outfile
def countLines(file, outfile):
outfile = open(outfile, "w")
count = 0
num = 0
numcount1 = []
numcount = []
for line in file:
spl = line.split(" ")
listx = list(spl)
counts = {}
for i in range(0, 10):
count[i] = count[i, 0] + str(listx.count(i))
for spl in line:
if spl.isalnum():
num = num + 1
else:
num = num + 0
pr = str(num)+": "+line+"\n"
outfile.write(pr)
count = count + 1
return count, counts
def printResults(count, counts):
print(count, counts)
main()