What I want on the first run thru is to ask for input, print input, ask for 2nd input, print 2nd input. End the first run, saving inputs to text file. My results are:
input 1
input 1 again.
input 2
input 2 again.

On the 2nd time thru, it works exactly the way I want it to:
print all inputs in one line, (ex: input 1.input2.), ask for 3rd input,
prints saved inputs + new input 3 and asks for input 4.

while True:
    oinp = open('open_inputs.txt','r')
    for line in oinp:
        print(line)
        
    inp = input('Enter text, or "Enter" to quit: ')
    oinp = open('open_inputs.txt','a')  
    oinp.write(inp, )
    print (inp)
    oinp.close()
    
    if inp == "":
        break

Thanks for any help you can give me.

It never has the first time through as it reads first and if the file does not exist it stops with error message.

Maybe because of the print imp? I'm not sure, this worked fine for me:

while True:
    oinp = open('open_inputs.txt','r')
    for line in oinp:
        print(line)
    oinp.close()
    print('')#I just needed to make it prettier   
    inp = input('Enter text, or "Enter" to quit: ')
    if inp == "":
        break
    else:
        oinp = open('open_inputs.txt','a')
        oinp.write(inp,)
        oinp.close()

Maybe because of the print imp? I'm not sure, this worked fine for me:

while True:
    oinp = open('open_inputs.txt','r')
    for line in oinp:
        print(line)
    oinp.close()
    print('')#I just needed to make it prettier   
    inp = input('Enter text, or "Enter" to quit: ')
    if inp == "":
        break
    else:
        oinp = open('open_inputs.txt','a')
        oinp.write(inp,)
        oinp.close()

ugh too late to edit. Anyways I meant to post this.

while True:
    oinp = open('open_inputs.txt','r')
    for line in oinp:
        print('')#prettier
        print(line)
    oinp.close()
    print('')#I just needed to make it prettier   
    inp = input('Enter text, or "Enter" to quit: ')
    if inp =="":
        break
    else:
        inp+='\n'
        oinp = open('open_inputs.txt','a')
        oinp.write(inp,)
        oinp.close()

Thank you for your prompt response. It no longer double prints my initial inputs, which is great, but I need one additional tweak. On the first run through, I only want to print the input only, one at a time.

Below is an example the output I need from the first run of the program.

Enter text: Python is fun.
Python is fun.
Enter text: It can also be challenging.
It can also be challenging.
Enter text:#End of first run through

The output from a second run of the program is perfect.

Python is fun.It can also be challenging.
Enter text: The file is saving correctly.
Python is fun.It can also be challenging.The file is saving correctly.
Enter text:

well if you're saying that you want it to do that prior to the input=='' then you could simply make two while loops changing the first to exclude the file reading, like:

while True:   
    inp = input('Enter text, or "Enter" to quit: ')
    if inp =="":
        break
    else:
        inp+='\n'
        oinp = open('open_inputs.txt','a')
        oinp.write(inp,)
        oinp.close()
while True:
    oinp = open('open_inputs.txt','r')
    for line in oinp:
        print('')#prettier
        print(line)
    oinp.close()
    print('')#I just needed to make it prettier   
    inp = input('Enter text, or "Enter" to quit: ')
    if inp =="":
        break
    else:
        inp+='\n'
        oinp = open('open_inputs.txt','a')
        oinp.write(inp,)
        oinp.close()

Although this is essentially repeating yourself, which is best to avoid.

Simple try...except around reading old file should suffice, with correct initialization of the list (alternatively you can create empty file in except in case of you can not read one in try). pyguy62's fix of your code is flaky and messy as he says (push enter in first loop at first run and you end doing again read of non-existing file same as before).

Can you help me with Try, Except. I have never used this before so am unfamiliar with what it does or how it works. Thanks

it is used to catch Errors, so you are saying try this except if an error is raised then do this.

try:
   dancing()
except BadDancerError:
   print('Please Stop Dancing')

Edited 5 Years Ago by pyguy62: n/a

This article has been dead for over six months. Start a new discussion instead.