我在同一目录中有以下3个单独的python文件:
with open('cds.txt', 'r') as d:
dcontents = d.readlines()
dcontents = [d.strip() for d in dcontents]
dcontents = filter(None, dcontents)
cd_list = []
for item in dcontents:
ac = create_cd(*item.split(';'))
cd_list.append(ac)
cdcodelist = []
for i, cd in enumerate(cd_list):
print('CD #{idx}: {c}, {n}, {g}, {p}, {a}, {y}'.format(idx=i+1, c=cd.code, n=cd.name, g=cd.genre,p=cd.pieces, a=cd.artist, y=cd.year))
cdcodeforlist = cd.code
cdcodelist.append(cdcodeforlist)
class create_cd:
cost = 1.5
rent = False
def __init__(self, code, name, genre, pieces, artist, year):
self.code = code
self.name = name
self.genre = genre
self.pieces = pieces
self.artist = artist
self.year = year
def __repr__(self):
return("(%d,%s,%s,%d,%.2f,%s,%d)"%(self.code, self.name, self.genre, self.pieces, self.cost, self.artist, self.year))
def setcode(self, code):
self.code = code
def setname(self, name):
self.name = name
def setyear(self, year):
self.year = year
def setcost(self, cost):
self.cost = cost
def setrent(self,rent):
self.setrent = rent
def setgenre(self, genre):
self.genre = genre
def setpieces(self, pieces):
self.pieces = pieces
def setartist(self, artist):
self.artist = artist
def getname(self):
return self.name
def getyear(self):
return self.year
def getcost(self):
return self.cost
def getgenre(self):
return self.genre
def getrent(self):
return self.rent
def getcode(self):
return self.code
def getartist(self):
return self.artist
def getpieces(self):
return self.pieces
from cdclass import *
class cdvalidate(create_cd):
def __repr__(self):
return("(%d,%s,%s,%d,%.2f,%s,%d)"%(self.code, self.name, self.genre, self.pieces, self.cost, self.artist, self.year))
def rentcd(self, days):
setrent(True)
return days*cost
def searchcd(code):
if rent == True:
print("CD is rented")
else:
print("CD is not rented")
此文本文件:
5412;不下雨;布鲁斯; 12;休劳瑞; 2000
5896;突然来;替代; 8;更近; 2016年875;必杀技;替代; 14;必杀技; 2002
我正在尝试创建一个导入3个cds的程序,然后有一个文本菜单,为用户提供租用CD的选项。我想出了以下代码:
daystorentcd = input("\nEnter the amount of days you with to rent the cd for:")
usercode = str(input("Enter the code of the cd"))
if usercode in cdcodelist:
cdvalidate.rentcd(daystorentcd)
else:
print("\ninvalid code try again:")
基本上在第一个文件中,它会在 cd_list 中创建一个包含所有cds的列表,然后使用 cdcodelist 来存储cds的所有代码以验证它们,以便用户尝试输入不存在的代码,显示错误。
接下来,我将用户希望租用的CD代码发送到相应的函数,并要求使用相同代码的cd的 setrent 值切换为True并返回总数租金的成本,以便用户以后可以选择查看哪些CD被租用,但这是菜单的另一部分,遗憾的是它不起作用,给我以下错误:
Traceback(最近一次调用最后一次):文件 “C:\ Users \ Yiannis \ Desktop \ Test project \ main.py”,第51行,中 cdvalidate.rentcd(daystorentcd)TypeError:rentcd()缺少1个必需的位置参数:'days'
我如何解决这个问题?我的代码中的错误在哪里?感谢任何帮助!