Home > Net >  While loop with break not working -> PYTHON
While loop with break not working -> PYTHON

Time:08-10

Can you guys help me on this? IDK why but it's not working when I type CLOSE.

I already solved another way to run the same code , but i want to know why this is not working

Thanks

file = open("testing.txt",' a')
while True:
    line = input("enter something: ")
    for i in line:
        if i == "CLOSE" :
            file.close()
            break
        else:
            file.write(line   "\n" )

CodePudding user response:

You've over-complicated this with the inner for loop. You just need:

with open('/tmp/foo.txt', 'a ') as f:
    while (ui := input('Enter something: ')) != 'CLOSE':
        print(ui, file=f)
  • Related