Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在终端中运行Python脚本_Python - Fatal编程技术网

在终端中运行Python脚本

在终端中运行Python脚本,python,Python,我是Python新手。现在我已经在XCode文本编辑器中创建了一个文件“testing.py”。它运行一个简单的程序,我希望能够在终端上运行它。到目前为止,我刚刚打开终端,键入“python”,然后一遍又一遍地重新键入相同的代码 此外,我尝试在终端中键入“python testing.py”,但它给了我一个错误: File "testing.py", line 22 break SyntaxError: 'break' outside loop 导致错误的代码段是: with open("tr

我是Python新手。现在我已经在XCode文本编辑器中创建了一个文件“testing.py”。它运行一个简单的程序,我希望能够在终端上运行它。到目前为止,我刚刚打开终端,键入“python”,然后一遍又一遍地重新键入相同的代码

此外,我尝试在终端中键入“python testing.py”,但它给了我一个错误:

File "testing.py", line 22
break
SyntaxError: 'break' outside loop
导致错误的代码段是:

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

有什么建议吗?

这不是为什么会出现
SyntaxError:'break'外部循环的答案

但是您的代码可以根据需要进行改进

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

您确实需要再使用
break

您确定代码缩进正确吗?错误很明显,如果它指向示例代码中的
break
,则缩进中肯定有错误。在OP提供的代码段中,可能存在重复,
break
正确地位于循环中。@RodXavier否,您链接的问题是关于不在循环中的中断,这里我们有一个loo中的中断对,错过了,谢谢你的更正。