Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code
#2
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.
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 deck
Now 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
Reply


Messages In This Thread
Code - by StoneCherry0119 - Oct-17-2022, 03:40 PM
RE: User choosing an entry from dictionary in randomly generated list - by deanhystad - Oct-17-2022, 04:04 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020