类:如何使用户可以将字典输入我的班级?

时间:2018-03-10 02:17:55

标签: python class dictionary typeerror

我需要编辑我的代码,而不是在下面自己创建包字典,而是用户可以输入自己的字典。对此的文档测试将如下:

>>> bag = BeadBag({'green':44, 'blue':20, 'yellow':15, 'red':11, 'white':2, 'black':1})
>>> bag.draw()
'yellow'
>>> bag.replace('yellow')

用户将字典输入到他们的通话中,我将不再需要“包”。

我随机继续

TypeError: 'int' object is not subscriptable 

TypeError: 'int' object is not iterable

我不时地尝试运行此代码。

我个人为了查看我的工作而亲自运行的文档测试是:

>>>bag = BeadBag()
>>>bag.draw()
'black'
>>>bag.replace('yellow')
{'red': [11], 'yellow': 23, 'green': [33], 'blue': [44], 'white': [55], 'black': 65}

但这并不是我所需要的。

import random
class BeadBag(object):
    """
    >>> bag = BeadBag({'green':44, 'blue':20, 'yellow':15, 'red':11, 'white':2, 'black':1})
    >>> bag.draw()
    'yellow'
    >>> bag.replace('yellow')
    >>>
    """
    bag = {'red':[11], 'yellow':[22], 'green':[33], 'blue':[44], 'white':[55], 'black':[66]}

    def draw(self):

        words = []
        for i in BeadBag.bag.keys():
            words.append(i)
        rand = random.choice(words)
        minus = ((BeadBag.bag.get(rand))[0]-1) #subtracts one from each draw
        for key, value in BeadBag.bag.items():
            BeadBag.bag[rand] = minus
        return rand

    def replace(self, color):
        cvalue = (BeadBag.bag[color])
        for i in cvalue:
            val = int(i)
        add = val+1
        for key, value in BeadBag.bag.items():
            BeadBag.bag[color] = add
        return BeadBag.bag

4 个答案:

答案 0 :(得分:0)

定义课程时,您需要提供__init__功能。这将使用参数,这些参数是您在创建类时提供给该类的项目。

class BeadBag():
    def __init__(self, input_dictionary):
        self.bag = input_dictionary

答案 1 :(得分:0)

首先,您需要使用self表示您的班级,而不是班级中的BeadBag

从这里开始。

import random


class BeadBag(object):
    def __init__(self, d):
        self.d = d
        self.words = d.keys()

    def draw(self):
        rand = random.choice(self.words)
        #self.d will be your dict. do whatever you want.

    def replace(self, color):
        cvalue = self.d[color]
        # rest of the code.

答案 2 :(得分:0)

如果我理解你想要在课堂上完成什么,可以这样做:

import random


class BeadBag(object):
    """
    >>> bag = BeadBag({'green':44, 'blue':20, 'yellow':15, 'red':11, 'white':2, 'black':1})
    >>> bag.draw()
    'yellow'
    >>> bag.replace('yellow')
    >>>
    """

    def __init__(self, d):
        self.bag = d

    def draw(self):
        rand = random.choice([k for k in self.bag.keys()])
        minus = self.bag.get(rand) - 1
        self.bag[rand] = minus

        return rand

    def replace(self, color):
        self.bag[color] += 1

样本用法:

>>> bag = BeadBag({'green':44, 'blue':20, 'yellow':15, 'red':11, 'white':2, 'black':1})
>>> bag.draw()
'red'
>>> bag.bag
{'green': 44, 'blue': 20, 'yellow': 15, 'red': 10, 'white': 2, 'black': 1}
>>> bag.replace('red')
>>> bag.bag
{'green': 44, 'blue': 20, 'yellow': 15, 'red': 11, 'white': 2, 'black': 1}

我假设您打算将bag字典存储在实例属性而不是第一类(与所有实例共享)。

draw方法从1中的随机密钥递减bag并返回递减的密钥。

replace方法只会从1增加bag[color]

答案 3 :(得分:0)

这里是如何在创建BeadBag类的实例时指定字典,并且每个实例都拥有它自己的bag字典属性。

这仍然没有通过doctest,但是因为draw)replace()方法中存在与您提出的问题无关的问题 - 并且我不清楚你到底想要做什么,所以他们仍然需要修理。

import ast
import doctest
import random

class BeadBag(object):
    """
    >>> bag = BeadBag("{'green':44, 'blue':20, 'yellow':15, 'red':11, 'white':2, 'black':1}")
    >>> bag.draw()
    'yellow'
    >>> bag.replace('yellow')
    >>>
    """

    def __init__(self, string):
        self.bag = ast.literal_eval(string)
        assert isinstance(self.bag, dict)

    def draw(self):
        words = []
        for i in self.bag.keys():
            words.append(i)
        rand = random.choice(words)
        minus = ((self.bag.get(rand))[0]-1) #subtracts one from each draw
        for key, value in self.bag.items():
            self.bag[rand] = minus
        return rand

    def replace(self, color):
        cvalue = (self.bag[color])
        for i in cvalue:
            val = int(i)
        add = val+1
        for key, value in self.bag.items():
            self.bag[color] = add
        return self.bag

if __name__ == '__main__':
    doctest.testmod()