-
Notifications
You must be signed in to change notification settings - Fork 0
/
junk.txt
85 lines (69 loc) · 2.43 KB
/
junk.txt
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import random
import string
import os
def generate_random_string(length=10):
"""
Generates a random string of specified length.
Parameters:
length (int): Length of the random string to generate.
Returns:
str: A random string of the specified length.
"""
letters = string.ascii_letters
return ''.join(random.choice(letters) for i in range(length))
def write_random_strings_to_file(filename, num_strings=5, string_length=10):
"""
Writes a specified number of random strings to a file.
Parameters:
filename (str): The name of the file to write to.
num_strings (int): The number of random strings to generate and write to the file.
string_length (int): The length of each random string.
"""
with open(filename, 'w') as file:
for _ in range(num_strings):
random_string = generate_random_string(string_length)
file.write(random_string + '\n')
print(f"Written {num_strings} random strings to {filename}")
def read_file_and_print(filename):
"""
Reads a file and prints its contents.
Parameters:
filename (str): The name of the file to read from.
"""
if os.path.exists(filename):
with open(filename, 'r') as file:
contents = file.read()
print(f"Contents of {filename}:\n{contents}")
else:
print(f"File {filename} does not exist.")
def random_number_operations():
"""
Performs random arithmetic operations on random numbers and prints the results.
"""
num1 = random.randint(1, 100)
num2 = random.randint(1, 100)
print(f"Random numbers: {num1}, {num2}")
sum_result = num1 + num2
diff_result = num1 - num2
prod_result = num1 * num2
if num2 != 0:
div_result = num1 / num2
else:
div_result = "undefined (division by zero)"
print(f"Sum: {sum_result}")
print(f"Difference: {diff_result}")
print(f"Product: {prod_result}")
print(f"Division: {div_result}")
def main():
"""
Main function to execute the random tasks.
"""
filename = "random_strings.txt"
# Write random strings to file
write_random_strings_to_file(filename)
# Read and print file contents
read_file_and_print(filename)
# Perform random number operations
random_number_operations()
if __name__ == "__main__":
main()