相关疑难解决方法(0)

如何将输入读作数字?

在下面的代码中为什么xy字符串而不是整数?网上的所有内容都说要使用raw_input(),但是我读input()raw_input()在Python 3.x中重命名的Stack Overflow(在一个不处理整数输入的线程上).

play = True

while play:

    x = input("Enter a number: ")
    y = input("Enter a number: ")

    print(x + y)
    print(x - y)
    print(x * y)
    print(x / y)
    print(x % y)

    if input("Play again? ") == "no":
        play = False
Run Code Online (Sandbox Code Playgroud)

python int input python-2.7 python-3.x

261
推荐指数
7
解决办法
76万
查看次数

TypeError:method()占用1个位置参数,但给出了2个

如果我上课了......

class MyClass:

    def method(arg):
        print(arg)
Run Code Online (Sandbox Code Playgroud)

...我用来创建一个对象......

my_object = MyClass()
Run Code Online (Sandbox Code Playgroud)

......我就这样打电话method("foo")......

>>> my_object.method("foo")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: method() takes exactly 1 positional argument (2 given)
Run Code Online (Sandbox Code Playgroud)

...为什么Python告诉我我给了它两个参数,当我只给出一个?

python methods arguments self python-3.x

210
推荐指数
8
解决办法
41万
查看次数

Python中整数的长度

在Python中,如何找到整数中的位数?

python integer count

204
推荐指数
10
解决办法
32万
查看次数

在类体中调用类staticmethod?

当我尝试在类的主体内使用静态方法,并使用内置staticmethod函数作为装饰器定义静态方法时,如下所示:

class Klass(object):

    @staticmethod  # use as decorator
    def _stat_func():
        return 42

    _ANS = _stat_func()  # call the staticmethod

    def method(self):
        ret = Klass._stat_func() + Klass._ANS
        return ret
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Traceback (most recent call last):<br>
  File "call_staticmethod.py", line 1, in <module>
    class Klass(object): 
  File "call_staticmethod.py", line 7, in Klass
    _ANS = _stat_func() 
  TypeError: 'staticmethod' object is not callable
Run Code Online (Sandbox Code Playgroud)

我理解为什么会发生这种情况(描述符绑定),并且可以通过_stat_func()在上次使用后手动转换为static方法来解决它,如下所示:

class Klass(object):

    def _stat_func():
        return 42

    _ANS = _stat_func()  # use the non-staticmethod version

    _stat_func = staticmethod(_stat_func) …
Run Code Online (Sandbox Code Playgroud)

python static-methods decorator

134
推荐指数
5
解决办法
9万
查看次数

TypeError:'NoneType'对象在Python中不可迭代

错误TypeError: 'NoneType' object is not iterable是什么意思?

我在这个Python代码上得到它:

def write_file(data, filename): # creates file and writes list to it
  with open(filename, 'wb') as outfile:
    writer = csv.writer(outfile)
    for row in data: # ABOVE ERROR IS THROWN HERE
      writer.writerow(row)
Run Code Online (Sandbox Code Playgroud)

python nonetype

127
推荐指数
6
解决办法
50万
查看次数

在Python中使用字符串格式打印元组

所以,我有这个问题.我得到了元组(1,2,3),我应该用字符串格式打印.例如.

tup = (1,2,3)
print "this is a tuple %something" % (tup)
Run Code Online (Sandbox Code Playgroud)

这应该用括号打印元组表示,比如

这是一个元组(1,2,3)

但我得到了TypeError: not all arguments converted during string formatting.

我能在世界上做到这一点吗?有点失去了这里,如果你们可以指出我正确的方向:)

python

116
推荐指数
10
解决办法
19万
查看次数

在python中减去两次

我有两个datetime.time值,exit而且enter我想做的事情如下:

duration = exit - enter
Run Code Online (Sandbox Code Playgroud)

但是,我收到此错误:

TypeError:不支持的操作数类型 - :'datetime.time'和'datetime.time

我该怎么做呢?一种可能的解决方案是将time变量转换为datetime变量,然后转换为子结构,但我相信你们必须有更好更清洁的方法.

python datetime

103
推荐指数
7
解决办法
10万
查看次数

Python函数作为函数参数?

Python函数可以作为另一个函数的参数吗?

说:

def myfunc(anotherfunc, extraArgs):
    # run anotherfunc and also pass the values from extraArgs to it
    pass
Run Code Online (Sandbox Code Playgroud)

所以这基本上是两个问题:

  1. 它是否允许?
  2. 如果是,如何在其他功能中使用该功能?我需要使用exec(),eval()或类似的东西吗?永远不需要弄乱它们.

BTW,extraArgs是anotherfunc参数的列表/元组.

python arguments function

103
推荐指数
4
解决办法
18万
查看次数

使用带有列表的Python字符串格式

s在Python 2.6.5中构造了一个字符串,它将具有不同数量的%s标记,这些标记与列表中的条目数相匹配x.我需要写出一个格式化的字符串.以下不起作用,但表明我正在尝试做什么.在此示例中,有三个%s令牌,列表有三个令牌.

s = '%s BLAH %s FOO %s BAR'
x = ['1', '2', '3']
print s % (x)
Run Code Online (Sandbox Code Playgroud)

我想输出字符串是:

1 BLAH 2 FOO 3 BAR

python string formatting list string-formatting

95
推荐指数
7
解决办法
18万
查看次数

如何将Nonetype转换为int或string?

我有一个Nonetype价值x,它通常是一个数字,但可能是None.我想用数字除以它,但Python提出:

TypeError: int() argument must be a string or a number, not 'NoneType'
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

python

87
推荐指数
5
解决办法
21万
查看次数