-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRead-Text-File.py
More file actions
32 lines (22 loc) · 1.22 KB
/
Read-Text-File.py
File metadata and controls
32 lines (22 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Author: Jessica Strait
# This project reads a random number text file created from my own text file generating code.
# First, we will tell the program to open the random.txt file. We will also tell is to read the first line of the file.
file = open(r'C:\Users\jessl\Desktop\random.txt', "r")
numbers = file.readline()
# Next, we will use two different variables to define the sum of the numbers and the quantity of numbers. We define them as zero.
total = 0
quantity = 0
# We will create a while loop to ensure that each line is read on our file. The while loop will include changing the string to
# integers, increasing the total by the new number, increasing the quantity by one, printing the new number, and defining our
# new line to be read to prevent an infinite loop.
while numbers != "":
randomNumber = int(numbers)
total += randomNumber
quantity +=1
print(randomNumber)
numbers = file.readline()
# We must remember to close our file once our loop completes.
file.close()
# Finally, we will print the quantity of numbers in the file and the total of the numbers.
print("There are", quantity, "random numbers in this file.")
print("The sum of these numbers is", str(total)+".")