Advertisement
biswasrohit20

fi

Mar 18th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 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. if len(num) > 0:
  19. total += int(num)
  20. num = ""
  21. if lines.index(line) == len(lines) - 1 and line.index(c) == len(line)-1:
  22. total += int(num)
  23. print("The original file")
  24. print(filename)
  25. print("has been counted and the sum of")
  26. print("all numbers is", total)
  27. print()
  28.  
  29. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement