Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This program will read a file of random numbers and display
- # the numbers with a line number followed by a colon.
- # It will also show all of the even and odd numbers
- # and the sum of the all numbers
- #header
- print("CSC 122-W1 - Program #6")
- print("By Victor A")
- print("November 23rd 2016")
- print('')
- def main():
- # Declare variables
- #line = ''
- nbrcnt = 0
- even_count = 0
- odd_count = 0
- even_total = 0
- odd_total = 0
- # Open numbers.txt file for reading
- infile = open('numbers.txt', 'r')
- print('The "numbers.txt" file:')
- print('-----------------------')
- for line in infile:
- number = int(line)
- nbrcnt += 1
- print(format(nbrcnt, '2d'), ') ', number)
- if number % 2 == 0:
- even_count += 1
- even_total += number
- print("Even; Count:", even_count)
- if number % 2 != 0:
- odd_count += 1
- print("Odd; Count:", odd_count)
- # Close file
- infile.close()
- # Call the main function.
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement