Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2016
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. # This program will read a file of random numbers and display
  2. # the numbers with a line number followed by a colon.
  3. # It will also show all of the even and odd numbers
  4. # and the sum of the all numbers
  5.  
  6. #header
  7. print("CSC 122-W1 - Program #6")
  8. print("By Victor A")
  9. print("November 23rd 2016")
  10. print('')
  11.  
  12.  
  13. def main():
  14. # Declare variables
  15. #line = ''
  16. nbrcnt = 0
  17. even_count = 0
  18. odd_count = 0
  19. even_total = 0
  20. odd_total = 0
  21.  
  22. # Open numbers.txt file for reading
  23. infile = open('numbers.txt', 'r')
  24.  
  25. print('The "numbers.txt" file:')
  26. print('-----------------------')
  27.  
  28. for line in infile:
  29. number = int(line)
  30. nbrcnt += 1
  31. print(format(nbrcnt, '2d'), ') ', number)
  32. if number % 2 == 0:
  33. even_count += 1
  34. even_total += number
  35. print("Even; Count:", even_count)
  36. if number % 2 != 0:
  37. odd_count += 1
  38. print("Odd; Count:", odd_count)
  39.  
  40.  
  41. # Close file
  42. infile.close()
  43.  
  44. # Call the main function.
  45. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement