我有一个包含许多函数的程序,我不知道如何将它们链接在一起:/
当我在python的shell上工作时,一切都很好,因为一切都在一个定义中,但是当我将它们划分为不同的定义时,它就不工作了。你知道吗
我有一个不同的金属,每个金属都有一个重量、值和名称,等等。我想为这些金属创建一个类,然后根据valuePerBar
和valuePerWeight
对它们进行降序排序
这些是我的金属:
这是我的课:
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_object
是一个私有类,它使我的工作更容易,它所做的只是为每个参数放置类型!例如:name-->;str和totalBars-->;int..等
我在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)
目前没有回答
相关问题 更多 >
编程相关推荐