0

I am new to Python. Right now I have created a file, "testing.py" in an XCode text editor. It runs a simple program, and I want to be able to run it in the Terminal. Up to now I have just been opening the Terminal, typing "python", and retyping the same code over and over.

Also, I tried typing "python testing.py" in the Terminal, and it gave me an error:

File "testing.py", line 22
break
SyntaxError: 'break' outside loop

The code snippet that caused an error was:

with open("truth.txt") as f:
    while True:
        i = f.read(1)
        if not i:
            break
        bitstring += bin(ord(i))[2:].zfill(8)

Any suggestions?

msvalkon
  • 11,887
  • 2
  • 42
  • 38
user3562967
  • 151
  • 3
  • 12
  • 1
    Are you sure your code is indented correctly? The error is very clear, and if it points to that `break` in your example code, there is definitely an error in your indentation. – msvalkon Apr 23 '14 at 09:42
  • 1
    possible duplicate http://stackoverflow.com/questions/2462566/python-break-outside-loop – Rod Xavier Apr 23 '14 at 09:42
  • In the snippet the OP provided, `break` is correctly in side the loop. – msvalkon Apr 23 '14 at 09:46
  • @RodXavier no, the question you linked is about a break that is not in a loop, here we have a break in a loo[p – Jasper Apr 23 '14 at 09:46
  • Oh, right. Missed that one. Thanks for the correction. – Rod Xavier Apr 23 '14 at 09:48
  • @msvalkon I checked, the code in the file is exactly the same as what I posted here. – user3562967 Apr 23 '14 at 09:53
  • @user3562967 What did you check with? Is the `break` in your snippet on line 22? – msvalkon Apr 23 '14 at 09:55
  • @msvalkon I just checked by looking at the file, yes the "break" statement is on line 22 – user3562967 Apr 23 '14 at 10:02
  • @user3562967 use a different editor. I'm relatively certain that `Xcode` is using whitespace in a weird way which results in incorrect indentation. Are you confusing Linux and OSX by the way? Just type `less testing.py` in the terminal and see if the indentation matches. – msvalkon Apr 23 '14 at 10:04
  • @msvalkon Good catch, I meant OSX. Alright I'll try. But does my current method of entering "python testing.py" work for what I want to do? – user3562967 Apr 23 '14 at 10:07
  • I can't reproduce the problem with the code from the post. IMHO the `break` statement is indented incorrectly re the other code. – Laur Ivan Apr 23 '14 at 10:08
  • @user3562967 yes that is the canonical way to run a python script from terminal. – msvalkon Apr 23 '14 at 10:10
  • I tried the person's suggestion below and it worked. The program runs fine, and I can run various "print" statements to see variable values. But I want to be able to access some of the variables in that code to manipulate and change their values. Is there any way I can do that? – user3562967 Apr 23 '14 at 10:36
  • Also I figured out the indentation error, I followed the advice here: http://stackoverflow.com/questions/492387/indentationerror-unindent-does-not-match-any-outer-indentation-level It was indented but I think with whitespace instead of a tab. – user3562967 Apr 23 '14 at 11:14

1 Answers1

0

This is not an answer why SyntaxError: 'break' outside loop raised.

But your code can be improved as

>>> with open("truth.txt") as f:
...     for i in iter(lambda:f.read(1), ''):
...         bitstring += bin(ord(i))[2:].zfill(8)

You done need to use break anymore.

emesday
  • 6,078
  • 3
  • 29
  • 46