Oct-17-2022, 04:04 PM
(This post was last modified: Oct-19-2022, 11:23 AM by deanhystad.)
Sorry, but that was a lot of coding that you're going to throw away.
You need a deck of Pokemon cards. You have a good start at that with your random_pokemon() function.
Now that we have a deck of cards, selecting a card by name is easy:
You need a deck of Pokemon cards. You have a good start at that with your random_pokemon() function.
def new_deck(count): """Make a random deck of "count" pokemon cards.""" deck = {} # random.sample() guarantees no duplicates. To allow duplicates, use random.choices() for i in random.sample(range(1, MAX_POKEMON, k=count): response = requests.get(f"https://pokeapi.co/api/v2/pokemon/{i}/).json card = {key:response[key] for key in ("name", "id", "height", "weight")} deck[card["name"] = card return deckNow that you have a shuffled deck of pokemon cards, it is easy to deal cards.
def deal(count, deck): return [deck.pop() for _ in range(count)]When a card is delt, you pop() it from the deck so that nobody else can have that card.
Now that we have a deck of cards, selecting a card by name is easy:
def select_pokemon(deck): """Select a pokemon by name. If card in deck, return card, else return None""" # Should I print available pokemon, or is this a blind guess like "go fish"? name = input("Enter pokemon name: ") for pokemon in deck: if name.lower() == pokemon["name"].lower(): return pokemon return None