# Arup Guha
# Writing to a File - Basic Example
# 4/10/2020
# Edited Multprobs on 11/19/2020 to only print problems with an answer of
# 25 or higher.

# For checking if a file exists.
import os.path
from os import path
import random

def makeProb():

    # Store result and numbers for product here.
    ans = 0
    nums = []

    # Keep going till we get a product big enough.
    while ans < 25:

        # Generate a new problem, clear my old problem.
        nums.clear()
        num1 = random.randint(1,9)
        num2 = random.randint(1, 9)

        # Add this problem and calculate its answer.
        nums.append(num1)
        nums.append(num2)
        ans = num1*num2

    # Here is the multiplication we want.
    return nums
        

if path.exists("multprobs2.txt"):
    print("Sorry, the file we were going to write to exists. Exit program.")

# Safe to write to file.
else:

    # How we open a file to write to.
    # Very important - make sure this file doesn't exist in the directory
    # that your python program is in.
    myFile = open("multprobs2.txt", "w")

    # Command to write to an output file is myFile.write(string)

    # 8 rows of problems
    for i in range(8):

        # Will store my problems.
        probs = []
        for j in range(10):
            probs.append(makeProb())

        # 10 problems on a line. Each problem takes 3 lines to write out.
        for j in range(10):
            myFile.write("   "+str(probs[j][0])+"\t")
        myFile.write("\n")

        # On the second line I write a times sign.
        for j in range(10):
            myFile.write(" x "+str(probs[j][1])+"\t")
        myFile.write("\n")

        # On the third line I draw the horizontal line.
        for j in range(10):
            myFile.write("----\t")
        myFile.write("\n")
        
        myFile.write("\n")
        myFile.write("\n")
        myFile.write("\n")

    # Close the file.
    myFile.close()
