在下面的代码中为什么x和y字符串而不是整数?网上的所有内容都说要使用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) 如果我上课了......
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告诉我我给了它两个参数,当我只给出一个?
当我尝试在类的主体内使用静态方法,并使用内置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) 错误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) 所以,我有这个问题.我得到了元组(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.
我能在世界上做到这一点吗?有点失去了这里,如果你们可以指出我正确的方向:)
我有两个datetime.time值,exit而且enter我想做的事情如下:
duration = exit - enter
Run Code Online (Sandbox Code Playgroud)
但是,我收到此错误:
TypeError:不支持的操作数类型 - :'datetime.time'和'datetime.time
我该怎么做呢?一种可能的解决方案是将time变量转换为datetime变量,然后转换为子结构,但我相信你们必须有更好更清洁的方法.
Python函数可以作为另一个函数的参数吗?
说:
def myfunc(anotherfunc, extraArgs):
# run anotherfunc and also pass the values from extraArgs to it
pass
Run Code Online (Sandbox Code Playgroud)
所以这基本上是两个问题:
BTW,extraArgs是anotherfunc参数的列表/元组.
我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
我有一个Nonetype价值x,它通常是一个数字,但可能是None.我想用数字除以它,但Python提出:
TypeError: int() argument must be a string or a number, not 'NoneType'
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?