无法将多个函数和一个类连接起来

时间:2014-10-25 16:12:33

标签: python python-3.x python-idle

我有一个包含许多功能的程序,我不知道如何将它们链接在一起:/

当我在使用python的shell时,一切都运行正常,因为一切都在一个定义中,但是当我将它们分成不同的定义时,它就不起作用了。

我有一个不同的金属,每个金属都有一个重量和价值以及一个名字等。我想为这些金属制作一个类,然后根据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_object是一个私有类,它使我的工作更轻松,它所做的就是为每个参数添加类型!例如:名字 - > str和totalBars - > int ..etc

我在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)

0 个答案:

没有答案