-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom.py
More file actions
25 lines (19 loc) · 792 Bytes
/
Copy pathRandom.py
File metadata and controls
25 lines (19 loc) · 792 Bytes
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
#EX6.4-QUEZON, ANGEL SIERRA C.
# PYTHON PROGRAMMING ACTIVITY
# WRITING A FILE
# Import random to generate random integers
import random
# Generate 100 random numbers, runs loop for 100 times
num = [random.randint(1, 1000) for _ in range(100)]
# Write the random numbers to data.txt, a file
with open("data.txt", "w") as data_file: # opens the file and inputs the generated random numbers in it
for number in num:
data_file.write(f"{number}\n")
# Sort the random numbers in increasing order
sortnum = sorted(num)
# Write the sorted numbers to sorted.txt
with open("sorted.txt", "w") as sorted_file:
for number in sortnum:
sorted_file.write(f"{number}\n")
print("Random numbers generated and saved in data.txt.")
print("Sorted random numbers saved in sorted.txt.")