Python Can';无法链接多个函数和一个类
我有一个包含许多功能的程序,我不知道如何将它们连接在一起:/ 当我使用python的shell时,一切都很好,因为一切都在一个定义中,但是当我将它们划分为不同的定义时,它就不起作用了 我有不同的金属,每种金属都有重量、值和名称等。我想为这些金属创建一个类,然后根据它们的降序对它们进行排序Python Can';无法链接多个函数和一个类,python,python-3.x,python-idle,Python,Python 3.x,Python Idle,我有一个包含许多功能的程序,我不知道如何将它们连接在一起:/ 当我使用python的shell时,一切都很好,因为一切都在一个定义中,但是当我将它们划分为不同的定义时,它就不起作用了 我有不同的金属,每种金属都有重量、值和名称等。我想为这些金属创建一个类,然后根据它们的降序对它们进行排序valuePerBar一次和valuePerWeight一次 这些是我的金属: 黄金,1巴,每巴重量5,每巴价值750 银,1巴,每巴1重,每巴400值 铑,1巴,每巴4重,每巴500值 铂金,1巴,每巴6重,
valuePerBar
一次和valuePerWeight
一次
这些是我的金属:
- 黄金,1巴,每巴重量5,每巴价值750
- 银,1巴,每巴1重,每巴400值
- 铑,1巴,每巴4重,每巴500值
- 铂金,1巴,每巴6重,每巴1000值
class Metal(rit_object):
"""
Represents a single metal type, composed of:
:slot name (str): The name of the metal
:slot totalBars (int): The total number of bars
:slot weightPerBar (int): The weight of a single bar
:slot valuePerBar (int): The value of a single bar
:slot valuePerWeight (float): The value per weight of the metal
:slot barsTaken (int): The number of bars added to the satchel
"""
__slots__ = ( 'name' , 'totalBars' , 'weightPerBar', 'valuePerBar', 'valuePerWeight', 'barsTaken' )
_types = ( str , int , int, int, float, int )
注意:rit\u object
是一个私有类,它使我的工作更容易,它所做的只是为每个参数输入类型!例如:名称-->str和totalBars-->整型..等
我在shell中进行排序的方式是:
class Metal(rit_object):
"""
Represents a single metal type, composed of:
:slot name (str): The name of the metal
:slot totalBars (int): The total number of bars
:slot weightPerBar (int): The weight of a single bar
:slot valuePerBar (int): The value of a single bar
:slot valuePerWeight (float): The value per weight of the metal
:slot barsTaken (int): The number of bars added to the satchel
"""
__slots__ = ( 'name' , 'totalBars' , 'weightPerBar', 'valuePerBar', 'valuePerWeight', 'barsTaken' )
_types = ( str , int , int, int, float, int )
platinum = Metal("platinum", 1, 6, 1000, 166.666667, 0 )
gold = Metal("gold", 1, 5, 750, 150.0, 0 )
rhodium = Metal("rhodium", 1, 4, 500, 125.0, 0 )
silver = Metal("silver", 1, 1, 4, 400.0, 0 )
Metals = [
Metal("platinum", 1, 6, 1000, 166.666667, 0 ),
Metal("gold", 1, 5, 750, 150.0, 0 ) ,
Metal("rhodium", 1, 4, 500, 125.0, 0 ),
Metal("silver", 1, 1, 4, 400.0, 0 )
]
def getKey(Metal):
return name.valuePerBar
sorted(customlist, key=getKey, reverse=True)
而且一切都很完美,现在我想制作一个具有多个定义的程序,使其组织良好,但我的问题是我不知道如何链接函数
这是我的节目:
"""
Author: Sean Strout (sps@cs.rit.edu)
Author: <<< YOUR NAME HERE >>>
This class represents the types of metal bars that Greedo can
store in his satchel. Each type of bar is a separate Metal
object. This module also has routines that work with metals,
e.g. creation, reading from a file, and sorting based on
various criteria.
Language: Python 3
"""
from rit_object import * # rit_object class
class Metal(rit_object):
"""
Represents a single metal type, composed of:
:slot name (str): The name of the metal
:slot totalBars (int): The total number of bars
:slot weightPerBar (int): The weight of a single bar
:slot valuePerBar (int): The value of a single bar
:slot valuePerWeight (float): The value per weight of the metal
:slot barsTaken (int): The number of bars added to the satchel
"""
__slots__ = ( 'name' , 'totalBars' , 'weightPerBar', 'valuePerBar', 'valuePerWeight', 'barsTaken' )
_types = ( str , int , int, int, float, int )
def createMetal(name, totalBars, weightPerBar, valuePerBar):
"""
Create and return a new Metal object.
:param name (str): The name of the metal
:param totalBars (int): The total number of bars
:param weightPerBar (int): The weight of a single bar
:param valuePerBar (int): The value of a single bar
:return: A newly initialized Metal object
:rtype: Metal
"""
platinum = Metal("platinum", 1, 6, 1000, 166.666667, 0 )
gold = Metal("gold", 1, 5, 750, 150.0, 0 )
rhodium = Metal("rhodium", 1, 4, 500, 125.0, 0 )
silver = Metal("silver", 1, 1, 4, 400.0, 0 )
def readMetals(Metals):
"""
Read the metals from a file whose format is:
metalName totalBars weightPerBar valuePerBar
:param fileName (str): The name of the file
:return: A list of Metal objects
:rtype: list
"""
Metals = [
Metal("platinum", 1, 6, 1000, 166.666667, 0 ),
Metal("gold", 1, 5, 750, 150.0, 0 ) ,
Metal("rhodium", 1, 4, 500, 125.0, 0 ),
Metal("silver", 1, 1, 4, 400.0, 0 )
]
print (name.valuePerBar)
def getKey(Metal):
return name.valuePerBar
def sortMetalsByValuePerBar():
"""
Sort the metals by value per bar using insertion sort. The list of
metals is modified in place to be ordered by value per bar.
:param metals (list of Metal): The list of metals
:return: None
:rtype: NoneType
"""
return sorted(Metals, key=getKey, reverse=True)
def getKey2(Metal):
return name.weightPerBar
def sortMetalsByValuePerWeight(metals):
"""
Sort the metals by value per weight using insertion sort. The list of
metals is modified in place to be ordered by value per weight.
:param metals (list of Metal): The list of metals
:return: None
:rtype: NoneType"""
return sorted(Metals, key=getKey, reverse=True)
def printMetals(metals):
"""
Display the metals to standard output.
:param metals (list of Metal): The list of metals
:return: None
:rtype: NoneType
"""
if Q == a:
getKey(Metal)
sortMetalsByValuePerBar()
else:
getKey2(Metal)
sortMetalsByValuePerWeight(metals)
Q = input ("Enter 'a' for descending order or 'b' for ascending order")
Metal(rit_object)
createMetal(name, totalBars, weightPerBar, valuePerBar)
readMetals(Metals)
printMetals(metals)
“”“
作者:肖恩·斯特劳特(sps@cs.rit.edu)
作者:>
此类表示Greedo可以使用的金属棒的类型
储存在他的背包里。每种类型的酒吧都是一种独立的金属
对象。该模块还具有处理金属的例程,
e、 g.创建、读取文件,并根据
各种标准。
语言:Python 3
"""
从rit_对象导入*#rit_对象类
金属类(rit_对象):
"""
表示单个金属类型,包括:
:插槽名称(str):金属的名称
:插槽totalBars(int):条的总数
:插槽重量PERBAR(int):单个杆的重量
:slot valuePerBar(int):单个条的值
:槽值每重量(浮动):金属的每重量值
:slot barsTaken(int):添加到背包中的条数
"""
__插槽(名称、totalBars、weightPerBar、valuePerBar、valuePerWeight、barsTaken)
_类型=(str,int,int,int,float,int)
def createMetal(名称、totalBars、weightPerBar、valuePerBar):
"""
创建并返回新的金属对象。
:param name(str):金属的名称
:param totalBars(int):条的总数
:param weightPerBar(int):单个条的重量
:param valuePerBar(int):单个条的值
:return:新初始化的金属对象
:rtype:金属
"""
铂=金属(“铂”,1,6,1000,166.666667,0)
黄金=金属(“黄金”,1,5750150.0,0)
铑=金属(“铑”,1,4500125.0,0)
银=金属(“银”,1,1,4400.0,0)
def readMetals(金属):
"""
从以下格式的文件中读取金属:
metalName totalBars重量PERBAR值PERBAR
:param fileName(str):文件名
:return:金属对象的列表
:rtype:list
"""
金属=[
金属(“铂”,1,6,1000,166.666667,0),
金属(“金”,1,5750150.0,0),
金属(“铑”,1,4500125.0,0),
金属(“银”,1,1,4400.0,0)
]
打印(名称.值PERBAR)
def getKey(金属):
返回name.valuePerBar
def sortMetalsByValuePerBar():
"""
使用插入排序按每条的值对金属进行排序
对金属进行适当的修改,以便按每巴的价值进行订购。
:param metals(金属列表):金属列表
:返回:无
:rtype:NoneType
"""
返回排序(金属,键=getKey,反转=True)
def getKey2(金属):
返回name.weightPerBar
def SORTMETALSBYVALUE每重量(金属):
"""
使用插入排序按每重量的值对金属进行排序
对金属进行适当的改性,以便按每重量的价值进行订购。
:param metals(金属列表):金属列表
:返回:无
:rtype:NoneType“”
返回排序(金属,键=getKey,反转=True)
def印刷金属(金属):
"""
将金属显示为标准输出。
:param metals(金属列表):金属列表
:返回:无
:rtype:NoneType
"""
如果Q==a:
getKey(金属)
sortMetalsByValuePerBar()
其他:
getKey2(金属)
SORTMETALSBYVALUE每重量(金属)
Q=输入(“输入'a'表示降序,输入'b'表示升序”)
金属(rit_对象)
CreateMetral(名称、totalBars、weightPerBar、valuePerBar)
阅读金属(金属)
印刷金属(金属)
您的问题是什么?如何链接这些函数,使它们在shell中工作!“它不起作用”有点模糊。。。你期望发生什么,正在发生什么,你尝试过什么?我期望金属按如下方式分类:金属(名称:铂金,totalBars:1,重量PerBar:6,价值PerBar:1000,价值PerWeight:166.666666,barsTaken:0)金属金属(名称:铑,道达尔:1,道达尔:5,道达尔:750,道达尔:150.0,道达尔:0)金属(名称:铑,道达尔:1,道达尔:4,道达尔:500,道达尔:125.0,道达尔:0)金属(名称:银,道达尔:1,道达尔:1,道达尔:400,道达尔:0)当我在shell中编写代码时会发生这种情况,但当我将其划分为函数时,它不会“将函数链接在一起”不是python中使用的术语,因此我仍然不清楚您想要什么。您能用不同的词来描述吗?您是在问如何在类方法中生成函数吗?