Last active
December 1, 2022 03:47
-
-
Save Dioarya/87ec69560d1188b0c2564f009635d181 to your computer and use it in GitHub Desktop.
1. Write a program, which given a number n = 1, 2, 3, ..., 10, creates a file named `table_n.txt` with the multiplication table of n 2. Write a program, which given a number n = 1, 2, 3, ..., 10, reads the file named `table_n.txt` and prints the numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Take in input from the user. | |
while True: | |
num = None | |
# Check if the input is a valid integer. | |
try: | |
# Take in input from the user. | |
num = input("Input a number: ") | |
int(num) # Try to turn input into an integer | |
except ValueError: | |
print("Please input a correct integer.") | |
continue | |
num = int(num) | |
if not (0 < num < 11): | |
print("Please input an integer in the range from 1 to 10.") | |
continue | |
break | |
# Open the file "timestable_n.txt" with mode of "w" to completely | |
# wipe the file each time the program is ran. | |
with open("timestable_n.txt", "w") as f: | |
for n in range(1, 10 + 1): | |
# Using an f string makes the code way easier to read. | |
f.write(f"{num} x {n} = {num * n}\n") | |
print(f"{num} x {n} = {num * n}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
if not os.path.exists("timestable_n.txt"): | |
print("The file \"timestable_n.txt\" does not exist in the current directory. \ | |
Please run the first program before running this one.") | |
exit(1) | |
while True: | |
num = None | |
# Check if the input is a valid integer. | |
try: | |
# Take in input from the user. | |
num = input("Input a number: ") | |
int(num) | |
except ValueError: | |
print("Please input a correct integer.") | |
continue | |
num = int(num) | |
if not (0 < num < 11): | |
print("Please input an integer in the range from 1 to 10.") | |
continue | |
break | |
# Open the file "timestable_n.txt" with mode of "r" to read the file. | |
with open("timestable_n.txt", "r") as f: | |
# Read the file. | |
data = f.read() | |
# Get each line of the file to be able take the index to find the requested multiplication. | |
lines = data.split("\n") | |
index = num - 1 | |
line = lines[index] | |
# Editor's note: If you only need the result and not the whole equation use this code --> | |
# From the line, we can get the result by getting the last integer in the line. | |
objects = line.split(" ") | |
result_string = objects[-1] | |
result = int(result_string) | |
print("The result is:", result) | |
# Here's the code to get the whole equation. | |
print(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment