我遇到了获取我需要的输出的问题。我使用一个模块(称为q3.py)与3定义的类:学生,模块和注册和执行它与下面的细节另一个文件:TypeError:str只需要2个参数错误
#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错误,但是我一定还有其他的东西,因为我没有得到任何接近我想要的输出的东西,尽管玩了很多年。任何帮助,将不胜感激!
您应该删除您的编辑,询问第二个问题,然后在另一个问题中提出第二个问题。就目前来看,你的编辑问题,你的标题是没有意义的,答案是没有意义的。这样做被认为是不好的形式......只是问第二个问题。 – SethMMorton
对不起,我是新人,不知道,我会记住未来。 – user3078840