Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def main():
- print("Program to count the total")
- print("of the numbers in a file.")
- print("You will be asked to enter")
- print("the name of a file.")
- print("Written by Raymond Zheng.")
- print()
- filename = input("Enter the name of a file to count: ")
- f = open(filename, "r")
- lines = f.readlines()
- total = 0
- for line in lines:
- num = ""
- for c in (line):
- if c.isdigit():
- num += c
- else:
- if len(num) > 0:
- total += int(num)
- num = ""
- if lines.index(line) == len(lines) - 1 and line.index(c) == len(line)-1:
- total += int(num)
- print("The original file")
- print(filename)
- print("has been counted and the sum of")
- print("all numbers is", total)
- print()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement