首页 > 解决方案 > 如何修复 SyntaxError:Raspberry Pi 上的无效语法?

问题描述

我在 Windows 上编写了代码,编译和工作正常,但在树莓派上失败

写了一些简单的测试代码,看看它是否吐出同样的错误

a = input('this is a test string to split at space \n').split()

print(a)

输入 15:00 (需要这个数据点用于更大的项目),我得到了这个错误

15:00
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    a = input('this is a test string to split at : \n').split(':')
   File "<string>", line 1
    15:00
      ^
 SyntaxError: invalid syntax

然后在不和谐的人的要求下尝试了 15 00,得到了这个消息

15 00
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    a = input('this is a test string to split at : \n').split(':')
  File "<string>", line 1
    15 00
        ^
SyntaxError: unexpected EOF while parsing

标签: python

解决方案


您可能正在运行从 Windows 到 Raspberry Pi 的不同版本的 python。

感谢这个答案

注意:这仅适用于 Python 2。对于 Python 3,raw_input() 已变为普通 input(),并且 Python 2 input() 已被删除。

参考

a = raw_input('this is a test string to split at space \n').split(':')

print(a)

在此处输入图像描述


推荐阅读