SyntaxError: invalid syntax - Python 3.5 [closed]

为君一笑 提交于 2019-12-11 20:03:36

问题


I have a small code in Python, which looks like that:

import sys
def _158a():
    n, k = map(int, sys.stdin.readline().split())
    data = input().split()
    a=[]
    for i in range(n):
        a.append(int(data[i]))
    ans=0
    for i in range(n):
        if a[i]>=a[k-1] and a[i]:
            ans+=1
    return ans

res = _158a()
print(res)

with input from keyboard:

3 3
1 2 3

When i run code with above input, i got an error:

Traceback (most recent call last):
  File "/Users/tranhieu/Desktop/Python/158A.py", line 14, in <module>
    res = _158a()
  File "/Users/tranhieu/Desktop/Python/158A.py", line 4, in _158a
    data = input().split()
  File "<string>", line 1
    1 2 3
      ^
SyntaxError: invalid syntax
Process finished with exit code 1

Can you help me fix this bug?


回答1:


You should use raw_input() instead of input(), since in Python 2.x input() tries to parse the input. In Python 3.x input() just returns a string like raw_input() in Python 2.x.



来源:https://stackoverflow.com/questions/34187161/syntaxerror-invalid-syntax-python-3-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!