I'm a beginner python programmer and new to this forum website. I've searched around and haven't been able to find what I've been looking for, so I came here.
I have a function that calculates the percent error between two values given from user input, writes that value to a file in the string: 'Trial x % error = y', where x is the trial number and y equals the float value of the percent error. Every time this function is run, it adds a new line to the file and writes, 'Trial (x 1) % error = y', this way it is easy to track your trials. The function then turns each line in the file into an attribute of a list, and prints to the console the last item in that list.
To fix this, I decided a way to do this was to: split each attribute in the list (which is a string of words) where a space is, and save each word in a line to its own list. This way I could index the second attribute in the new list and save it to the value x, add 1 to it, and that would be the new value of x.
def percentError():
x = 1
#defining formula
error = str(100 * (measured - accepted) / (accepted))
#opening % Error.txt
errorTxt = open('% Error.txt', 'a')
#writing in file
errorTxt.write('Trial ' str(x) ' % error = ' error '%')
#new line
errorTxt.write('\n')
#close file
errorTxt.close()
#open file again
errorTxt = open('% Error.txt', 'r')
#read from and print what is read
trial = []
trialNum = []
for line in errorTxt:
trial.append(line.strip())
print(trial[-1])
for attribute in trial[-1]:
z = trial.split(' ')
trialNum.append(z)
x = trialNum[1]
#file close
errorTxt.close()
There's code above the comment (#defining formula) that has user inputs for the variables of measured and accepted, I left it out for readability.
This gives me the error - AttributeError: 'list' has no attribute 'split'
Anyone know how to fix my problem? The goal is to have x only increase by 1 from the highest trial number in the file.
CodePudding user response:
You can write a simple tokenizer with the re module. Maybe like this:
import re
def simple_tokenize(text):
return re.sub(r"\W", " ", text).split()
Then you can turn your list of text strings into a list of list of words like:
data_in = [
"To be, or not to be, that is the question:",
"Whether ’tis nobler in the mind to suffer",
"The slings and arrows of outrageous fortune,",
"Or to take arms against a sea of troubles,",
"And by opposing end them?"
]
data_out = [simple_tokenize(line) for line in data_in]
print(data_out)
Giving you:
[
['To', 'be', 'or', 'not', 'to', 'be', 'that', 'is', 'the', 'question'],
['Whether', 'tis', 'nobler', 'in', 'the', 'mind', 'to', 'suffer'],
['The', 'slings', 'and', 'arrows', 'of', 'outrageous', 'fortune'],
['Or', 'to', 'take', 'arms', 'against', 'a', 'sea', 'of', 'troubles'],
['And', 'by', 'opposing', 'end', 'them']
]
Looking at your specific code and your description of what you want to do in the end, I think there is a lot of it you can remove and a bunch of the work/splitting/parsing you are doing is redundant.
If you want to:
- Calculate the percent error between two values given from user input.
- writes that value to a file in the string: 'Trial x % error = y'
- the function to determine trial based on prior data
You might try:
def percentError(measured, accepted):
error = 100 * (measured - accepted) / (accepted)
with open('percent_error.txt', 'a ') as error_file:
error_file.seek(0)
try:
last_line = error_file.readlines()[-1]
last_trial_number = int(last_line.split()[1])
except (ValueError, IndexError):
last_trial_number = 0
last_line = f"Trial {last_trial_number 1} % error = {error}%"
error_file.write(f"{last_line}\n")
print(last_line)
percentError(10,8)
percentError(10,9)
percentError(10,7)
user_measured = int(input("enter measured: ")) ## note this is brittle and will error if the user enters bad data
user_accepted = int(input("enter accepted: ")) ## note this is brittle and will error if the user enters bad data
percentError(user_measured, user_accepted)
Should show you:
Trial 1 % error = 25.0%
Trial 2 % error = 11.11111111111111%
Trial 3 % error = 42.857142857142854%
and a 4th trial based on your user input.
then it will generate a file "percent_error.txt" with the same results
