我在运行以下代码时遇到错误:
class Person:
def _init_(self, name):
self.name = name
def hello(self):
print 'Initialising the object with its name ', self.name
p = Person('Constructor')
p.hello()
Run Code Online (Sandbox Code Playgroud)
输出是:
Traceback (most recent call last):
File "./class_init.py", line 11, in <module>
p = Person('Harry')
TypeError: this constructor takes no arguments
Run Code Online (Sandbox Code Playgroud)
有什么问题?
我一直在研究Python The Hard Way并且我得到了上述错误,并且不知道为什么.我拿出了大部分我认为可能的填充文字.对不起,如果有点长.
from sys import exit
from random import randint
class Game(object):
def __int__(self, start):
self.quips = [
"You died. You kinda suck at this.",
"Your mom would be proud. If shw were smarter.",
"Suck a loser.",
"I have a small puppy that's better at this."
]
self.start = start
def play(self):
next_room_name = self.start
while True:
print "\n-------"
room = getattr(self, next_room_name)
next_room_name = room()
def death(self):
print self.quips[randint(0, len(self.quips)-1)]
exit(1)
def central_corridor(self):
print "The Gothons of …Run Code Online (Sandbox Code Playgroud) 我经常从我的 Python 代码中得到未捕获的异常(错误),这些异常被描述为TypeErrors. 经过大量的实验和研究,我收集了以下示例(以及细微的变化):
TypeError: func() takes 0 positional arguments but 1 was given
TypeError: func() takes from 1 to 2 positional arguments but 3 were given
TypeError: func() got an unexpected keyword argument 'arg'
TypeError: func() missing 1 required positional argument: 'arg'
TypeError: func() missing 1 required keyword-only argument: 'arg'
TypeError: func() got multiple values for argument 'arg'
TypeError: MyClass() takes no arguments
TypeError: unsupported operand type(s) for +: 'int' and 'str'
TypeError: can only concatenate str …Run Code Online (Sandbox Code Playgroud) 我正在完成本教程.我正在迭代地完成这个工作.此时我有以下二进制类:
class Binary:
def __init__(self,value):
self.value = str(value)
if self.value[:2] == '0b':
print('a binary!')
self.value= int(self.value, base=2)
elif self.value[:2] == '0x':
print('a hex!')
self.value= int(self.value, base=16)
else:
print(self.value)
return int(self.value)
Run Code Online (Sandbox Code Playgroud)
我正在使用pytest运行一系列测试,包括:
def test_binary_init_hex():
binary = Binary(0x6)
assert int(binary) == 6
E TypeError: int() argument must be a string or a number, not 'Binary'
Run Code Online (Sandbox Code Playgroud)
我问了一个关于这个TypeError的问题:int()参数必须是一个字符串或数字,而不是'二进制'并且基于答案将代码更改为如上所述.现在当我使用pytest运行测试套件时,所有测试都失败了,错误是:
TypeError: __init__() should return None, not 'int'
Run Code Online (Sandbox Code Playgroud)
为什么会有问题?
我只是想制作一个生成骰子的代码(在python中).这是代码:
import random
class Dice:
def _init_(self, number_dice):
self._dice = [6] * number_dice
def roll_dice(self):
for d in range(len(self._dice)):
self._dice[d] = random.randit(1, 6)
self._dice.sort()
def print_roll(self):
length = len(self._dice)
print(str(lenth) + "dice:" + str(self._dice))
my_dice = Dice(2)
my_dice.roll_dice()
my_dice.print_roll()
Run Code Online (Sandbox Code Playgroud)
编译器对第18行说了些什么.我是编程的新手,所以任何事情都有帮助=]
现在我正在开发一个程序,允许人们进行测试,将它们保存到数据库,然后打印它们.我一直收到错误:
Traceback (most recent call last):
File "C:/Users/Shepard/Desktop/Gradebook.py", line 50, in <module>
qs = QuestionStorage("questions.db")
TypeError: object.__new__() takes no parameters
Run Code Online (Sandbox Code Playgroud)
任何人都有任何想法?我假设它在QuestionStorage类的某个地方,但我不能完全解决任何问题.这是我第一次使用SQLite3,而且我遇到了很多麻烦,如果有人可以帮助我,那就太棒了.:)
import sqlite3
class QuestionStorage(object):
def _init_(self, path):
self.connection = sqlite3.connect(path)
self.cursor = self.connection.cursor()
def Close(self):
self.cursor.close()
self.connection.close()
def CreateDb(self):
query = """CREATE TABLE questions
(id INTEGER PRIMARY KEY, Question TEXT, Answer1 TEXT, Answer2 TEXT, Answer3 TEXT, Answer4 TEXT, CorrectAnswer TEXT)"""
self.cursor.exeute(query)
self.connection.commit()
#self.cursor.close()
def AddQuestion(self, Question, Answer1, Answer2, Answer3, Answer4):
self.cursor.execute("""INSERT INTO questions
VALUES (?, ?, ?, ?, ?, …Run Code Online (Sandbox Code Playgroud) 我试图在Python中实现一个队列.但是每次我运行我的代码时,我都会收到消息"AttributeError:Queue实例没有属性'队列'"我已经挣扎了一个多小时左右.非常感谢任何帮助.
我的代码:
class Queue:
def __int__(self):
'''initilize a empty queue'''
self.queue = []
def dequeue(self):
'''remove and return the last element'''
return self.queue.pop()
def enqueue(self, val):
'''Add element to the end'''
self.queue.append(val)
def is_empty(self):
'''Return True if empty queue'''
return len(self.queue) == 0
if __name__== '__main__':
q = Queue()
for i in range(0,11):
q.enqueue(i)
while not q.is_empty():
print q.dequeue()
Run Code Online (Sandbox Code Playgroud) 我正在设置一个类,并且第一步使用__init__函数来初始化属性。但是,当我尝试从该类创建实例时,它显示AttributeError。
我已经一遍又一遍地检查代码,看语法是否有问题,但错误仍然存在
class RandomWalk():
def ___init___(self, points = 10):
"""initialize attributes of a walk"""
self.points = points
self.x_values = [0]
self.y_values = [0]
rw = RandomWalk()
print(rw.points)
Run Code Online (Sandbox Code Playgroud)
我期望输出10作为默认值points,但错误显示:
Traceback (most recent call last):
File "test1.py", line 10, in <module>
print(rw.points)
AttributeError: 'RandomWalk' object has no attribute 'points'
Run Code Online (Sandbox Code Playgroud)
如果我用或替换属性points,问题仍然存在x_valuesy_values