问题
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