2016-12-08 10 views
-2

私はenemyPokemonという4つの辞書を持っていますが、敵が行うことができる辞書があります。 '別の変数を得るために変数と文字列を一緒に追加するには

あなたが戦っている敵のポケモンに応じて、ムーブリストからランダムなムーブを選択しようとしていますが、その方法はわかりません。

これは私が書いたコードです:

xは4敵ポケモンのいずれかを選択する変数です。

enemyPokemon = { 
    1: 'Slowpoke', 
    2: 'Eevee', 
    3: 'Piplup', 
    4: 'Rattata', 
} 

SlowpokeFight = { 
    1:'Water Pulse', 
    2:'Zen Headbutt', 
    3:'Tackle', 
    4:'Rain Dance', 
} 
EeveeFight = { 
    1:'Sand Attack', 
    2:'Bite', 
    3:'Double-Edge', 
    4:'Last Resort', 
} 
PiplupFight = { 
    1:'Water Sport', 
    2:'Peck', 
    3:'Bubble', 
    4:'Drill Peck', 
} 
RattataFight = { 
    1:'Tail Whip', 
    2:'Quick Attack', 
    3:'Hyper Fang', 
    4:'Crunch', 
} 


randomMove = random.randint(1,4) 
whatEnemy = str(enemyPokemon[int(x)])+'Fight') 
print (str(whatEnemy[int(randomMove)])) 
print (randomMove) 
print (whatEnemy[int(x)]) 
+0

はここでエラー何ですか? update:2行目のコード 'whatEnemy = str(enemyPokemon [int(x)])+ 'Fight'' –

+1

あなたの投稿を編集して使用している辞書を含めることができますどのように物事を設定し、これを行うために変更する必要があるかもしれません。 – coralvanda

答えて

0

代わりに入れ子の辞書を使用するように変更すると、あなたのポケモンにあなたの動きをマッピングすることができます。

2つの辞書に入れることで、このようにすることができます。

import random 

enemyPokemon = { 
    1: 'Slowpoke', 
    2: 'Eevee', 
    3: 'Piplup', 
    4: 'Rattata', 
} 

pokemonMoves = { 
    'Slowpoke' : { 
     1:'Water Pulse', 
     2:'Zen Headbutt', 
     3:'Tackle', 
     4:'Rain Dance' 
    }, 
    'Eevee' : { 
     1:'Sand Attack', 
     2:'Bite', 
     3:'Double-Edge', 
     4:'Last Resort' 
    } 
    # ... 
} 

randomMove = random.randint(1, 4) 
enemy = enemyPokemon[randomMove] 
move = pokemonMoves[enemy][randomMove] 

print(enemy) 
print(move) 

あなたも、キーと値としての動きの辞書としてポケモン名のただ一つの辞書を持つように傾斜させてもよいです。あなたは文字列から変数を呼び出したい場合は、これは何が必要です

import random 

pokemon = { 
    'Slowpoke' : { 
     1:'Water Pulse', 
     2:'Zen Headbutt', 
     3:'Tackle', 
     4:'Rain Dance' 
    }, 
    'Eevee' : { 
     1:'Sand Attack', 
     2:'Bite', 
     3:'Double-Edge', 
     4:'Last Resort' 
    } 
    # ... 
} 

# creates a list of all the keys (pokemon names from dict) 
allPokemon = list(pokemon) 

randomMove = random.randint(1, 4) 
enemy = random.choice(allPokemon) 
move = pokemon[enemy][randomMove] 

print(enemy) 
print(move) 
0

whatEnemy = globals()[(str(enemyPokemon[int(x)])+'Fight')] 
関連する問題