Advertisement
biswasrohit20

file

Mar 18th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. def main():
  2. print("Program to count the total")
  3. print("of the numbers in a file.")
  4. print("You will be asked to enter")
  5. print("the name of a file.")
  6. print("Written by Raymond Zheng.")
  7. print()
  8. filename = input("Enter the name of a file to count: ")
  9. f = open(filename, "r")
  10. lines = f.readlines()
  11. total = 0
  12. for line in lines:
  13. num = ""
  14. for c in (line):
  15. if c.isdigit():
  16. num += c
  17. else:
  18. total += int(num)
  19. num = ""
  20. if lines.index(line) == len(lines) - 1 and line.index(c) == len(line)-1:
  21. total += int(num)
  22. print("The original file")
  23. print(filename)
  24. print("has been counted and the sum of")
  25. print("all numbers is", total)
  26. print()
  27.  
  28. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement