0
f = open("calculator.txt", "a+")

a = True

while a == True:

    operation = input("Please input an operation (+, -, *, /): ")
    num1 = float(input("Please input number 1: "))
    num2 = float(input("Please input number 2: "))
    if operation == "+":
       result = num1 + num2
    elif operation == "-":
        print(num1 - num2)
    elif operation == "*":
        print(num1 * num2)
    elif operation == "/":
        print(num1 / num2)
    answer = input("Run again? (Yes/No): ")
    if answer == "Yes":
        continue
    else:
        print("Goodbye ")
    exit()

How do i go about writing the results of the calculations in a file? Say the user inputs 5 + 5 and the program returns 10, how would I save "10" in a text file? If possible the entire equation "5+5 = 10"

Rafael de Bem
  • 641
  • 7
  • 15
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). Stack Overflow is not intended to replace existing tutorials and documentation. We expect you to use the available materials before posting here. – Prune Oct 10 '20 at 16:31

5 Answers5

3

You can use str.format to format result string and then use print() with file= parameter to write to file. For example:

with open('operations.txt', 'a') as f_out:
    while True:
        operation = input("Please input an operation (+, -, *, /): ")
        num1 = float(input("Please input number 1: "))
        num2 = float(input("Please input number 2: "))
        if operation == "+":
            result = num1 + num2
        elif operation == "-":
            result = num1 - num2
        elif operation == "*":
            result = num1 * num2
        elif operation == "/":
            result = num1 / num2

        result_string = '{} {} {} = {}'.format(num1, operation, num2, result)

        # print result string to screen:
        print(result_string)

        # save result string to file
        print(result_string, file=f_out)

        answer = input("Run again? (Yes/No): ")
        if answer == "Yes":
            continue
        else:
            print("Goodbye ")
            break

Prints:

Please input an operation (+, -, *, /): +
Please input number 1: 1
Please input number 2: 2
1.0 + 2.0 = 3.0
Run again? (Yes/No): Yes
Please input an operation (+, -, *, /): /
Please input number 1: 2
Please input number 2: 4
2.0 / 4.0 = 0.5
Run again? (Yes/No): no
Goodbye 

And saves operations.txt:

1.0 + 2.0 = 3.0
2.0 / 4.0 = 0.5
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
1

You could do it like this:

    f.write(f'{num1} {operation} {num2} = {result}') # for example, '5 + 5 = 10'
    if answer == "Yes":
        continue
    else:
        print("Goodbye ")
Rafael de Bem
  • 641
  • 7
  • 15
1

you can change your program like this:

f = open("calculator.txt", "a+")

a = True

while a:
    operation = input("Please input an operation (+, -, *, /): ")
    num1 = float(input("Please input number 1: "))
    num2 = float(input("Please input number 2: "))
    if operation == "+":
       result = num1 + num2
    elif operation == "-":
       result = num1 - num2
    elif operation == "*":
       result = num1 * num2
    elif operation == "/":
        result = num1 / num2
    
    print(result)
    
    f.write(f"{num1}{operation}{num2} = {result}\n") #this will save the whole equation
    answer = input("Run again? (Yes/No): ")

    if answer == "Yes":
        continue
    else:
        print("Goodbye ")
        a = False
fayez
  • 370
  • 1
  • 5
0

Just do like this:

with open("calculator.txt", "a+") as myfile:
  myfile.write("\n" + result)

Inside if block

Wasif
  • 14,755
  • 3
  • 14
  • 34
0

You can open the file and write into it like this:

f = open("calculator.txt", "a+")
f.write(f"{num1} {operation} {num2} = {result}")
seshadri_c
  • 6,906
  • 2
  • 10
  • 24
gnv
  • 1
  • 1