我创建了类需要帮助关联类的值

时间:2014-03-20 13:10:21

标签: python class python-2.7

不确定我是否在标题中有意义,但我已经创建了类,现在我需要将一个类与一个数字相关联。例如,1将是便士(对象),并且对于此对象,我具有相关的高度和重量。我需要帮助,询问如何将此类与数字1相关联。

我知道我需要让它像问题那样提出问题

print "Enter 1 for Penny\nEnter 5 for Nickel\nEnter 10 for Dime\nEnter 25 for Quarter\nEnter 50 for halfdollar\nEnter 100 for Dollar"

我需要它然后让我输入数字: 比你输入的数字中有多少硬币: 比给我总重量 比给我总高度

我应该将它包含在原始的打印声明中,还是应该做另一个打印声明,或者只是保持原样...

print "Enter 1 for Penny\nEnter 5 for Nickel\nEnter 10 for Dime\nEnter 25 for Quarter\nEnter 50 for halfdollar\nEnter 100 for Dollar\nEnter how many coins"

现在输入多少硬币的最后一部分如何将其与一个类相关联我为Penny设置了一个类,例如

#! /usr/bin/python
# Defining a class
class Coin():
    def __init__(slef,weight,height):
      self.weight = weight
      self.height = height

class Penny(Coin)
  def __init__(self)
    Coin.__init__(self,2.500,1.52)
    Penny = 1

class Nickel(Coin):
  def __init__(self):
    Coin.__init__(self,5.00,1.95)
    Nicke = 5

class Dime(Coin):
  def __init__(self):
    Coin.__init__(self,2.268,1.35)
    Dime = 10

class Quarter(Coin):
  def __init__(self):
    Coin.__init__(self,5.670,1.75)
    Quarter = 25

class HalfDollar(Coin):
  def __init__(self):
    Coin.__init__(self,11.34,2.15)
    HalfDollar = 50

class Dollar(Coin):
  def __init__(self):
    Coin.__init__(self,8.10,2.00)
    Dollar = 100
 print "Enter 1 for Penny\nEnter 5 for Nickel\nEnter 10 for Dime\nEnter 25 for Quarter\nEnter 50 for halfdollar\nEnter 100 for Dollar\n"
 print "Please enter a number:"

4 个答案:

答案 0 :(得分:0)

我认为你要做的是这样的事情:

class Coin():
    def __init__(self, name, value, weight, height):
      self.name = name
      self.value = value
      self.weight = weight
      self.height = height

definitions = [("dollar", 100, 8.1, 2.0), ...] # define the coins

coins = {data[1]: Coin(*data) for data in definitions} # create Coin instances

Dollar不需要成为Coin的子类,您可以将其设为具有适当属性值的实例。

您现在可以按值Coin访问coins内的每个dollar = coins[100] 实例。

for value in sorted(coins):
    print("Enter {0.value} for a {0.name}".format(coins[value]))
coin_value = int(raw_input("Pick a coin: "))
coin = coins[coin_value]
coin_count = int(raw_input("How many {0}: ".format("{0.name}s".format(coin)
                                                   if coin.value > 1 else
                                                   "pennies")))

或使用它来访问名称:

{{1}}

答案 1 :(得分:0)

如果您将其命名为cents之类的通用名称,则可以按如下方式创建查找表:

lookup_table = {Penny.cents: Penny, Nickle.cents: Nickle, Dime.cents: Dime, ... }
coin = lookup_table[number_user_entered]()

通过这种方式,您可以根据用户输入动态创建适当类的实例。

答案 2 :(得分:0)

其他答案都是处理问题的好方法。要按照你的定义方式严格解决它:

class Coin():
    def __init__(self, weight, height):
        self.weight = weight
        self.height = height

class Penny(Coin):
    value = 1
    def __init__(self):
        Coin.__init__(self, 2.500, 1.52)

class Nickel(Coin):
    value=5
    def __init__(self):
        Coin.__init__(self, 5.00, 1.95)

class Dime(Coin):
    value=10
    def __init__(self):
        Coin.__init__(self, 2.268, 1.35)

class Quarter(Coin):
    value=25
    def __init__(self):
        Coin.__init__(self, 5.670, 1.75)

class HalfDollar(Coin):
    value=50
    def __init__(self):
        Coin.__init__(self, 11.34, 2.15)

class Dollar(Coin):
    value=100
    def __init__(self):
        Coin.__init__(self, 8.10, 2.00)

mycoin = Penny()
print "A penny is worth", mycoin.value

yourcoin = Quarter()
print "A quarter is worth", yourcoin.value

association = {str(class_obj.value): class_obj for class_obj in (Penny, Nickel, Dime, Quarter, HalfDollar, Dollar)}

print "Enter 1 for Penny\nEnter 5 for Nickel\nEnter 10 for Dime\nEnter 25 for Quarter\nEnter 50 for halfdollar\nEnter 100 for Dollar\n"
value = raw_input("Please enter a number:")
number = raw_input("How many of that coin:")

if value in association:
    mycoin = association[value]()
    print "total weight:", mycoin.weight * int(number)
    print "total height:", mycoin.height * int(number)

答案 3 :(得分:0)

如何将硬币类型和硬币数量存储到单个多维列表的字典中:

coins = {1:[Penny,[]], 5:[Nickel,[]], 10:[Dime,[]]} # etc...

然后,当您向用户询问硬币类型和金额时,将该值作为字典的键查找并将多个硬币附加到其列表中,在此过程中实例化该类:

coin_type = raw_input("Enter 1 for Penny\nEnter 5 for Nickel\nEnter 10 for Dime\n:")
coin_amnt = raw_input("Enter amount: ")
for i in range(coin_amnt):
    coins[coin_type][1].append(coins[coin_type][0]())

检查硬币类型的总金额:

len(coins[coin_type][1])

检查硬币类型的个别实例:

for c in coins[coin_type][1]:
    print c.weight, c.height

如果您想打印硬币的类型,可以为每个硬币类添加__str __()特殊方法,然后在循环列表时打印实例。

希望这有帮助。