0

I am using python to develop a basic calculator. My goal is to create an output file of results for a series. I've tried looking for the correct command but haven't had any luck. Below is my current code. I believe all i need it to finish the "def saveSeries(x):" but I am not sure how to. I have created the input file already in the respective folder.

import os

import sys

import math

import numpy
.
.
.
.
def saveSeries(x):


def readSeries():
    global x
    x = []
    input file = open("input.txt", "r")
    for line in inputFile:
        x.append(float(line))
cherries
  • 1
  • 2

2 Answers2

1

I believe this question is asking about the saveSeries function. I assume the file structure you want is the following:

1
2
3
4
5

Your solution is very close, but you'll want to iterate over your number list and write a number for each line.


def saveSeries(x):
    
    outfile = open('output.txt', 'w')

    for i in x:
        outfile.write(str(i) + "\n") # \n will create a new line
        
    outfile.close()

I also noticed your readSeries() was incorrect. Try this instead and call .readlines().

def readSeries():
    global x
    x = []
    inputFile = open("input.txt", "r")
    for line in inputFile.readlines():
        x.append(float(line))
    
    inputFile.close()



Ryno_XLI
  • 161
  • 9
0

There are lots of ways you could go about accomplishing your goal, so you have to decide how you want to implement it. Do you want to write/update your file every time your user performs a new calculation? Do you want to have a save button, that would save all of the calculations performed?

One simple example:

# Create New File If Doesn't Exist
myFile = open('myCalculations.txt', 'w')
myFile.write("I should insert a function that returns my desired output and format")
myFile.close()
rastawolf
  • 338
  • 2
  • 11
  • Thank you! Yes the goal would be to write/update the output file everytime a new calculation is performed. – cherries Nov 01 '21 at 04:38
  • Take the output of your calculation and assign it to a variable. You are already printing the output to the console, so this should be a simple task. Use that variable as the argument you pass to `myFile.write()` and call this function at the end of each one of your functions called in 1-9. Once you get comfortable with that, tweak it so that it performs exactly how you want it to. Maybe you want to assign new file names after the program terminates? Maybe you want to include a timer for how long the program ran for? Or how long it took to perform calculations? Have fun with it! – rastawolf Nov 01 '21 at 04:46