Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 继承函数(?)_Python_Python 3.x - Fatal编程技术网

Python 继承函数(?)

Python 继承函数(?),python,python-3.x,Python,Python 3.x,我在同一目录中有以下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_lis

我在同一目录中有以下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)


连同此文本文件:

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)
5412;不是下雨了吗;忧郁;12;休·劳里;2000年

5896;突然来了;可供替代的8.更接近;2016年

875;涅槃;可供替代的14;涅槃;2002年

我试图创建一个程序,导入3张cd,然后有一个文本菜单,用户可以选择租用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_列表中所有cd的列表,然后使用cdcodelist存储cd的所有代码以验证它们,因此如果用户试图输入一个不存在的代码,它将显示一个错误

接下来,我将用户希望租用的CD的代码发送到相应的功能,并要求使用相同代码的CD的setrent值切换为True并返回租用的总成本,以便用户稍后可以选择查看租用的CD,但这是菜单的另一部分,遗憾的是它不起作用,给我以下错误:

回溯(最近一次调用上次):文件 “C:\Users\Yiannis\Desktop\Test project\main.py”,第51行,在 cdvalidate.rentcd(daystorentcd)类型错误:rentcd()缺少1个必需的位置参数:“天”


我如何解决这个问题?我的代码中的错误在哪里?非常感谢您的帮助

您不应该编写哑getter和setter,只需直接访问属性。
cdvalidate
是一个类,而
rentcd
是一个实例方法;您需要在
cdvalidate
的实例上调用它。但是,如果不看最后的代码片段如何与其他内容相匹配,就不可能告诉您如何修复此问题。@NilsWerner您能详细说明一下吗?谢谢。@John.E这是基本的面向对象编程。听上去你会更满意。@John.E Nils-Werner告诉你停止编写像
setname
getname
这样的方法。只需使用
self.name
。Python中不需要getter和setter。
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:")