SyntaxError:无效语法 - Python 3.5 [英] SyntaxError: invalid syntax - Python 3.5

查看:56
本文介绍了SyntaxError:无效语法 - Python 3.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 中有一段小代码,如下所示:

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)

通过键盘输入:

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?

推荐答案

你应该使用 raw_input() 而不是 input(),因为在 Python 2.x input() 尝试解析输入.在 Python 3.x 中 input() 只返回一个类似于 Python 2.x 中的 raw_input() 的字符串.

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.

这篇关于SyntaxError:无效语法 - Python 3.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆