Hi everyone! New here and to Python. I'm having trouble with my break statement executing and cannot figure out why. I'm learning to write to files and want the loop to break if the user hits enter. It's not working for some reason. I've even tried to get fancy (in my mind at least) and added other options to cause a break and still nothing! Any advice, help, or push in the right direction would be greatly appreciated.

f = open('my_file.txt', 'a')
f.close()
break_point = ('\n', '', ' ')
string_input = 'Enter text: '

while True:
    open_file = open('my_file.txt', 'r').readlines()
    for i in open_file:
        print(i)

    get_input = input(string_input).strip
    
    if get_input in break_point:
        break
	
    open_file = open('my_file.txt', 'a')
    open_file.write(get_input())
    open_file.close()

Recommended Answers

All 2 Replies

Hi everyone! New here and to Python. I'm having trouble with my break statement executing and cannot figure out why. I'm learning to write to files and want the loop to break if the user hits enter. It's not working for some reason. I've even tried to get fancy (in my mind at least) and added other options to cause a break and still nothing! Any advice, help, or push in the right direction would be greatly appreciated.

f = open('my_file.txt', 'a')
f.close()
break_point = ('\n', '', ' ')
string_input = 'Enter text: '

while True:
    open_file = open('my_file.txt', 'r').readlines()
    for i in open_file:
        print(i)

    get_input = input(string_input).strip
    
    if get_input in break_point:
        break
	
    open_file = open('my_file.txt', 'a')
    open_file.write(get_input())
    open_file.close()

You must CALL the strip() method. Your line 11 returns an object which is never in break_point !

You must CALL the strip() method. Your line 11 returns an object which is never in break_point !

Thank you Gribouillis! That was it. I forgot the dang parens!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.