0

I am trying to create a program that takes the start times and end times of word phrases, subtracts them to find the time length, and writes the length of each phrase into a file. i would like the final result to be "The length of phrase #(NumTrials) is: (length)" with length and numtrials being variables. I have tried a couple different approaches, but python is telling me that it is expecting only 1 argument for the write function. how could i modify this so that each phrase has it's length documented on a new line each time?

from decimal import Decimal, getcontext
getcontext().prec = 4
def main():
    myfile = open('Phrases Compiled.txt', 'w')
    NumTrials = 0
    SumLength = 0
    StoreLengths = dict()
    response = input("are there more calculations you'd like to do?")
    while response != "no":
        if response in ['yes', 'y', 'Yes', 'Y']:
        start, stop = eval(input("what is the start and stop times of the phrase?"))
        start = Decimal(start)
        stop = Decimal(stop)
        length = stop - start
        StoreLengths[NumTrials] = length
        NumTrials += 1
        SumLength += length
        print(length)
        length = str(length)
        NumTrials = str(NumTrials)
        myfile.write("the length of phrase #", NumTrials, "is: ",length"\n")
        response = input("are there more calculations you'd like to do?")
    elif response in ['no', 'n', 'N', 'No']:
        print("calculations are done")
        break 
averagelength = SumLength/NumTrials
print("average length was:", averagelength)

main()

1
  • question has been resolved, thank you to all those who responded Commented Jun 26, 2017 at 18:53

3 Answers 3

3

You are doing concatenation wrong...

myfile.write("the length of phrase #", NumTrials, "is: ",length"\n")

Should be

myfile.write("the length of phrase #" + str(NumTrials) + "is: " + str(length) + "\n")
# But even with "+" I guess it would be wrong to add strings and decimal objects together... Hence the "str(...)" but it is kinda bad.

So the best option:

myfile.write("the length of phrase #%d is: %f\n" % (NumTrials, length))

Edit: I guess you also misindented your code after the if response in ['yes', ...] because Python should not allow that formating.

Sign up to request clarification or add additional context in comments.

Comments

1

Personally when formatting strings i like to use string.format()

myfile.write("the length of phrase #", NumTrials, "is: ",length"\n")

Would become

myfile.write("the length of phrase {} is: {}\n".format(NumTrials, length))

Comments

0

I think you forgot to concatenate the string correctly. This should solve the problem:

from decimal import Decimal, getcontext
getcontext().prec = 4
def main():
    myfile = open('Phrases Compiled.txt', 'w')
    NumTrials = 0
    SumLength = 0
    StoreLengths = dict()
    response = input("are there more calculations you'd like to do?")
    while response != "no":
        if response in ['yes', 'y', 'Yes', 'Y']:
        start, stop = eval(input("what is the start and stop times of the phrase?"))
        start = Decimal(start)
        stop = Decimal(stop)
        length = stop - start
        StoreLengths[NumTrials] = length
        NumTrials += 1
        SumLength += length
        print(length)
        length = str(length)
        NumTrials = str(NumTrials)
        myfile.write("the length of phrase #", NumTrials, "is: ", length + "\n") # Note that it's length + "\n"
        response = input("are there more calculations you'd like to do?")
    elif response in ['no', 'n', 'N', 'No']:
        print("calculations are done")
        break 
averagelength = SumLength/NumTrials

print("average length was:", averagelength)

Edit: Question already answered

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.