Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
will this work
#3
I don't think you can use random.randint() to draw a card, at least not if you want it to mimic drawing a card from a physical deck. What you've written is more like rolling a die. The difference is that when you draw cards from a deck you remove the cards. Those particular cards cannot be drawn again, and drawing a card changes the probability of what card is drawn next. When rolling a die the next roll is not affected in any way by the previous roll

To draw cards from a deck you should create a deck of cards, shuffle the deck, take cards off the top of the deck.
import random

# Create deck of cards that contains 8 infantry, 8 tanks, 4 planes
deck = ["infantry"] * 8 + ["tank"] * 8 + ["plane"] * 4
random.shuffle(deck)
print(deck)
Output:
['tank', 'plane', 'tank', 'infantry', 'tank', 'tank', 'infantry', 'infantry', 'tank', 'infantry', 'plane', 'tank', 'infantry', 'infantry', 'infantry', 'plane', 'plane', 'tank', 'infantry', 'tank']
To draw a card from the deck just pop() off the first card. Now the deck contains 1 less card.
Reply


Messages In This Thread
will this work - by AGM - Aug-29-2022, 08:41 AM
RE: will this work - by Yoriz - Aug-29-2022, 09:15 AM
RE: will this work - by deanhystad - Aug-29-2022, 05:34 PM

Forum Jump:

User Panel Messages

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