0

これは、私が今日尋ねた質問 ( "List" Object Not Callable, Syntax Error for Text-Based RPG ) に連結されています。今、私のジレンマは、プレイヤーのハーブ リストにハーブを追加することにあります。

self.herb = []

開始ハーブリストです。関数 collectPlants:

def collectPlants(self):
    if self.state == 'normal':
    print"%s spends an hour looking for medicinal plants." % self.name
        if random.choice([0,1]):
        foundHerb = random.choice(herb_dict)
        print "You find some %s." % foundHerb[0]
        self.herb.append(foundHerb)
        print foundHerb
    else: print"%s doesn't find anything useful." % self.name

foundHerb がランダムに選択されます。このアイテムをきちんとした方法でリストに追加するにはどうすればよいですか (現在、ハーブの名前が表示されてから「なし」と表示されます)、同じハーブを複数持つことができるようにするにはどうすればよいですか?

ハーブクラスは次のとおりです。

class herb:
    def __init__(self, name, effect):
        self.name = name
        self.effect = effect

ハーブのサンプルリスト (警告: 未成熟):

herb_dict = [
    ("Aloe Vera", Player().health = Player().health + 2),
    ("Cannabis", Player().state = 'high'),
    ("Ergot", Player().state = 'tripping')
]
4

2 に答える 2

3

Use a list.

self.herb = []
foundHerb = 'something'
self.herb.append(foundHerb)
self.herb.append('another thing')
self.herb.append('more stuff')

print 'You have: ' + ', '.join(self.herb)
# You have: something, another thing, more stuff

EDIT: I found the code from which you get foundHerb in one of your other questions (please post it in this question too!), which is:

foundHerb = random.choice(herb_dict)

When I look at herb_dict:

herb_dict = [
    ("Aloe Vera", Player().health == Player().health + 2),
    ("Cannabis", Player().state == 'high'),
    ("Ergot", Player().state == 'tripping')
]
  1. This is wrong, use = for assignment. == is for testing equality.
  2. You need to use a function in the second item in these tuples.
  3. Don't add the second item into the list. Like this:

    self.herb.append(foundHerb[0])
    
于 2013-07-19T09:06:44.180 に答える
0

random.choice([0,1])あなたの関数で、が だったらどうなるか考えてみてください0。ブロックを実行しないifため、ハーブが選択されることはありません。おそらく、あなたの機能では、return Falseハーブは見つからなかったと言えます。次に、これを行うことができます:

self.herb = []
myherb = collectPlants() # This will either contain a herb or False
if myherb: # If myherb is a plant (and it isn't False)
    self.herb.append(myherb)
于 2013-07-19T09:13:51.597 に答える