相关疑难解决方法(0)

Python舍入到最接近的0.25

我想将整数舍入到最接近的0.25十进制值,如下所示:

import math

def x_round(x):
    print math.round(x*4)/4

x_round(11.20) ## == 11.25
x_round(11.12) ## == 11.00
x_round(11.37) ## == 11.50
Run Code Online (Sandbox Code Playgroud)

这在Python中给出了以下错误:

Invalid syntax
Run Code Online (Sandbox Code Playgroud)

python math rounding python-3.x

3
推荐指数
2
解决办法
4681
查看次数

为什么这个语法无效?

为什么抱怨语法无效?

#! /usr/bin/python

recipients = []
recipients.append('chris@elserinteractive.com')

for recip in recipients:
    print recip
Run Code Online (Sandbox Code Playgroud)

我一直在:

File "send_test_email.py", line 31
    print recip
              ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

python printing syntax-error python-3.x

2
推荐指数
1
解决办法
8448
查看次数

python脚本无法显示"调用'print'时缺少括号"

我从python.org网站下载了python.然后我试图运行以下程序.

print "Hello World !"
Run Code Online (Sandbox Code Playgroud)

但是有一个错误,比如"调用'print'时缺少括号".为什么我收到此错误?

在此输入图像描述

python-3.x

2
推荐指数
1
解决办法
4万
查看次数

“print file.read()”Python 3 中的无效语法

我想创建一个 10 个文件,其中每个文件的第一句话都有一个“blob”词,然后直接阅读这些句子。这是我的代码:

import random
import string

for i in range(9):
    name = input('fileNumber')+ str(i+1) + '.txt'
    try:
        file = open(name,'w+')
        file = open(name,'a')
        file.write("blob")
        file = open(name,'r')
        print file.read()       #'file' being highlighted with red color when I execute
        file.close()
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我收到一条错误消息Invalid syntax,它突出显示了我的file.read()行。

有人可以告诉我代码中的缺陷在哪里吗?

编辑:我目前使用的是 python 3.5。但是,我也可以切换到 2.7!

python

2
推荐指数
1
解决办法
7964
查看次数

遇到格式化程序的问题:"%支持的操作数类型:'NoneType'和'tuple'"

我收到这个错误,我不明白为什么.

我正在学习使用Gmail的API并复制粘贴他们的示例:https://developers.google.com/gmail/api/v1/reference/users/threads/get#examples

这是代码:

def GetThread(service, user_id, thread_id):
  """Get a Thread.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    thread_id: The ID of the Thread required.

  Returns:
    Thread with matching ID.
  """
  try:
    thread = service.users().threads().get(userId=user_id, id=thread_id).execute()
    messages = thread['messages']
    print ('thread id: %s - number of messages '
           'in this thread: %d') % (thread['id'], len(messages))
    return thread
  except errors.HttpError, error:
    print 'An …
Run Code Online (Sandbox Code Playgroud)

python

2
推荐指数
1
解决办法
277
查看次数

>> 不支持的操作数类型:'builtin_function_or_method' 和 '_io.TextIOWrapper'

这是我的代码:

def _parse(self, text):
    """
    This is the core interaction with the parser.

    It returns a Python data-structure, while the parse()
    function returns a JSON object
    """

    # CoreNLP interactive shell cannot recognize newline
    if '\n' in text or '\r' in text:
        to_send = re.sub("[\r\n]", " ", text).strip()
    else:
        to_send = text


    self.corenlp.sendline(to_send)
    max_expected_time = max(300.0, len(to_send) / 3.0)

    # repeated_input = self.corenlp.except("\n")  # confirm it
    t = self.corenlp.expect(["\nNLP> ", pexpect.TIMEOUT, pexpect.EOF,
                                 "\nWARNING: Parsing of sentence failed, possibly because of …
Run Code Online (Sandbox Code Playgroud)

python json unsupportedoperation

2
推荐指数
1
解决办法
5816
查看次数

pycharm中"预期结束语"

为什么我收到此错误

为什么我在pycharm中收到此错误(请参阅linting),说"预期语句结束"

我是python的新手.

python numpy pycharm python-3.x

2
推荐指数
1
解决办法
1万
查看次数

为什么这些Python脚本会出现语法错误?

我试图在Windows上安装python,这是我在python上的第一天.在Windows 7 x64上安装顺利.但几乎所有脚本都失败了.我正在尝试安装芹菜并在celery文件夹上运行以下命令.

python setup.py build
Run Code Online (Sandbox Code Playgroud)

它失败了,以下是一个错误

  File "setup.py", line 40
except ImportError, exc:
                      ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

也跟着失败,这是我认为有效的打印命令.

>>> print 'a'
  File "<stdin>", line 1
    print 'a'
            ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

我相信我在这里遗漏了一些东西.知道是什么让它失败了吗?

编辑:下面是我为了让python工作而必须完成的任务的总结,为自己做了笔记,但是如果它可以帮助任何人的话也把它放在这里

Install python and celery
=========================
-celery does not work with python3, so install latest python2
-install windows install for python2
-add C:\python2X to %PATH%
-set python path for lib
        set PYTHONPATH=%PYTHONPATH%;c:\python2x
-install setuptools
    http://pypi.python.org/pypi/setuptools
    for x64 install does not work use
        python setup.py install
-then can …
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
1万
查看次数

找不到我的Python 3.3语法错误

我收到以下错误:

  File "foo.py", line 6
    print "This implementation requires the numpy module."
                                                         ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

在这个Python代码中:

#!/usr/bin/python

try:
    import numpy
except:
    print "This implementation requires the numpy module."
    exit(0)

###############################################################################

if __name__ == "__main__":
    R = [
         [1,2,3],
         [4,5,6]
        ]
Run Code Online (Sandbox Code Playgroud)

怎么了?

编辑:我使用Python 3.3

python python-3.x

1
推荐指数
1
解决办法
196
查看次数

SyntaxError在Python 3中没有例外

我正在尝试在我的程序顶部编写一个代码块,如果程序意外地在Python 2.x中运行,它将给出一条错误消息并退出,但如果在Python中运行3.x将正常运行:

try:
    print "Error: This program should only be run in Python 3."
    raw_input('>')
    exit()
except SyntaxError:
    pass
print("I see you're running Python 3.")
# rest of program
Run Code Online (Sandbox Code Playgroud)

这在Python 2.7中正常工作(即,它显示错误和退出),但是当我在Python 3.3中运行它时,我得到一个SyntaxError,即使我告诉它有一个例外.

  File "version.py", line 2
    print "This program should only be run in Python 3"
                                                      ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

python syntax-error python-2.7 python-3.x

1
推荐指数
1
解决办法
249
查看次数