Python TypeError:str正好接受2个参数错误
我在获取所需的输出方面遇到了问题。我正在使用一个模块(称为q3.py),其中包含3个已定义的类:学生、模块和注册,并将其实现到另一个文件中,其中包含以下详细信息:Python TypeError:str正好接受2个参数错误,python,string,class,Python,String,Class,我在获取所需的输出方面遇到了问题。我正在使用一个模块(称为q3.py),其中包含3个已定义的类:学生、模块和注册,并将其实现到另一个文件中,其中包含以下详细信息: #testq3.py from q3 import * james = Student('james') alice = Student('alice') mary = Student('mary') agm = Module('agm') ipp = Module('ipp') r = Registrations() r.ad
#testq3.py
from q3 import *
james = Student('james')
alice = Student('alice')
mary = Student('mary')
agm = Module('agm')
ipp = Module('ipp')
r = Registrations()
r.add(james,agm)
r.add(alice,agm)
r.add(alice,ipp)
mstr = ''
for m in map(str,r.modules(alice)):
mstr = mstr+' '+m
print(alice, 'is doing the following modules:', mstr)
sstr = ''
for s in map(str,r.students(agm)):
sstr = sstr+' '+s
print(agm, 'has the following students:', sstr)
print(r)
这是我的q3.py文件
class Student:
'Class to define student details'
def __init__(self, name):
self.name = name
def __str__(self, name):
return (self.name)
def __iter__(self, name):
return iter(self.name)
class Module:
'Class to define module codes'
def __init__(self, modules):
self.modules = modules
def __str__(self, modules):
return self.modules
def __iter__(self, modules):
return iter(self.modules)
class Registrations():
'Class telling us what modules students are taking'
def __init__(self):
self.reglist = []
def add(self, students, modules):
self.reglist.append((students, modules))
def students(self, modules):
for x in self.reglist:
if x[1] == modules:
print x[0]
def modules(self, students):
for y in self.reglist:
if y[0] == students:
print y[1]
我不断收到错误,例如:
Traceback (most recent call last):
for m in map(str,r.modules(alice)):
File ".....", line 37, in modules
print y[1]
TypeError: __str__() takes exactly 2 arguments (1 given)
因此,我加入了str和iter,因为我不断得到参数2的迭代错误。我哪里做错了?我只希望输出与testq3.py底部的输出相同。请帮忙??我很确定我有很多str/iter错误,但肯定还有一些我遗漏的东西,因为尽管我已经玩了很长时间,但我还是没有接近我想要的输出。任何帮助都将不胜感激 您的
\uuuu str\uuu
方法需要一个额外的参数,但Python在打印对象时不会传递该参数
例如:
def __str__(self, modules):
return self.modules
def __str__(self):
return self.modules
您需要确保所有\uuuu str\uuu
方法都有def\uuuu str\uuuu(self):
作为签名。例如:
def __str__(self, modules):
return self.modules
def __str__(self):
return self.modules
您的
\uu str\uuuuuuuuuuu
和\uuuuuu iter\uuuuuuuuu
方法使用了一个不必要的模块
/名称
参数。Python不会从str
函数中传递这个,您也没有使用它,所以只需从这些方法的参数列表中删除它。啊,您建议我将其更改为什么?您应该删除提出第二个问题的编辑,然后在另一个问题中提出第二个问题。就你现在编辑的问题而言,你的标题毫无意义,答案也毫无意义。这样做被认为是不好的方式。。。再问第二个问题。对不起,我是新来的,不知道,以后我会记得的。