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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.