I am trying to make a Pokemon guesser game, where the names of the pokemon are stored in a list, and their types are stored in another list, as seen below:
gen1 = ["Bulbasaur", "Ivysaur", "Venusaur", "Charmander ", "Charmeleon", "Charizard", "Squirtle", "Wartortle", "Blastoise", "Caterpie", "Metapod", "Butterfree", "Weedle", "Kakuna", "Beedrill", "Pidgey", "Pidgeotto", "Pidgeot", "Rattata", "Raticate", "Spearow", "Fearow", "Ekans", "Arbok", "Pikachu", "Raichu", "Sandshrew", "Sandslash", "Nidoran♀", "Nidorina", "Nidoqueen", "Nidoran♂", "Nidorino", "Nidoking", "Clefairy", "Clefable", "Vulpix", "Ninetales"]
gen1types = ["grass", "grass", "grass", "fire ", "fire", "fire/dragon", "water", "water", "water", "bug", "bug", "bug", "bug", "bug", "bug", "flying", "flying", "flying", "normal", "normal", "flying", "flying", "poison", "poison", "electric", "electric", "ground", "ground", "poison", "poison", "poison", "poison", "poison", "poison", "fairy", "fairy", "fire", "fire"]
I want it to be so when a random pokemon is chosen from list gen1, the matching type is chosen along with it, for example if Bulbasaur is chosen, the matching type of that would be grass, I have written their types in the same order the pokemon are written, but I cannot get the correct type to match with the pokemon that is randomly chosen.. I tried to use gen1.index() but I would get an errors or the wrong type would be chosen. Can anyone help me with this? here is my full code so far:
import random
life = 4
genone = ["Bulbasaur", "Ivysaur", "Venusaur", "Charmander ", "Charmeleon", "Charizard", "Squirtle", "Wartortle", "Blastoise", "Caterpie", "Metapod", "Butterfree", "Weedle", "Kakuna", "Beedrill", "Pidgey", "Pidgeotto", "Pidgeot", "Rattata", "Raticate", "Spearow", "Fearow", "Ekans", "Arbok", "Pikachu", "Raichu", "Sandshrew", "Sandslash", "Nidoran♀", "Nidorina", "Nidoqueen", "Nidoran♂", "Nidorino", "Nidoking", "Clefairy", "Clefable", "Vulpix", "Ninetales"]
genonetypes = ["grass", "grass", "grass", "fire ", "fire", "fire/dragon", "water", "water", "water", "bug", "bug", "bug", "bug", "bug", "bug", "flying", "flying", "flying", "normal", "normal", "flying", "flying", "poison", "poison", "electric", "electric", "ground", "ground", "poison", "poison", "poison", "poison", "poison", "poison", "fairy", "fairy", "fire", "fire"]
genone[0,1] = genonetypes[]
x = random.choice(genone)
.index(str(x))y = random.choice(genonetypes)
print(y)
print(str("The pokemon type is " y))
userin = input("enter they pokemon you think it is: ")
if userin == x:
print("yes")
CodePudding user response:
zip
is what you need. It pairs up corresponding elements from 2 (or more) iterables, allowing you to get a pair via random.choice
.
import random
life = 4
genone = ["Bulbasaur", "Ivysaur", "Venusaur", "Charmander ", "Charmeleon", "Charizard", "Squirtle", "Wartortle", "Blastoise", "Caterpie", "Metapod", "Butterfree", "Weedle", "Kakuna", "Beedrill", "Pidgey", "Pidgeotto", "Pidgeot", "Rattata", "Raticate", "Spearow", "Fearow", "Ekans", "Arbok", "Pikachu", "Raichu", "Sandshrew", "Sandslash", "Nidoran♀", "Nidorina", "Nidoqueen", "Nidoran♂", "Nidorino", "Nidoking", "Clefairy", "Clefable", "Vulpix", "Ninetales"]
genonetypes = ["grass", "grass", "grass", "fire ", "fire", "fire/dragon", "water", "water", "water", "bug", "bug", "bug", "bug", "bug", "bug", "flying", "flying", "flying", "normal", "normal", "flying", "flying", "poison", "poison", "electric", "electric", "ground", "ground", "poison", "poison", "poison", "poison", "poison", "poison", "fairy", "fairy", "fire", "fire"]
x, y = random.choice(list(zip(genone,genonetypes)))
# print(y)
print(str("The pokemon type is " y))
userin = input("enter they pokemon you think it is: ")
if userin == x:
print("yes")
The list()
is needed as zip
only returns an iterator, which doesn't work as an input to random.choice
since random.choice
needs to be able to take the length of the input.
CodePudding user response:
Just get the random number of the index, something like :
idx = random.randint(0, len(genone))
pokemon = genone[idx]
pokemon_type = genonetypes[idx]
Alternatively, instead of two lists, I would use a dictionary to associate a pokemon to its element :
pokemons = {
'Bulbasaur' : 'grass',
'Charmander': 'fire',
#...
}
And then pick one entry randomly :
random.choice(list(pokemons.items()))
Yields :
('Charmander', 'fire')
CodePudding user response:
index
works fine:
x = random.choice(genone)
y = genonetypes[genone.index(x)]
print(x, y)
Though it's more efficient to get a random index and use it for both lists:
i = random.randrange(len(genone))
x = genone[i]
y = genonetypes[i]
print(x, y)