0

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()
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by joeywheels
0

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 !

0

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!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.