Python Forum
"Card Dealing" Python code 2.7
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Card Dealing" Python code 2.7
#1
import random
#Simulate the process of dealing of 1000 cards
print "Card dealing simulator"
print "By:"
Club = 0
Dia = 0
Heart = 0
Spade = 0
ClubC = 0
DiaC = 0
HeartC = 0
SpadeC = 0
Count = 0
while True:
    Cards = (random.random() * 52) + 1
    Count = Count + 1
    if 0 < Cards < 14:
        ClubC = ClubC + 1
    elif 14 < Cards < 27:
        DiaC = DiaC + 1
    elif 26 < Cards < 40:
        HeartC = HeartC + 1
    elif 39 < Cards < 53:
        SpadeC = SpadeC + 1
    elif Count == 1000:
        print "1000 cards have been dealt"
        break
print "There were " + str(ClubC) + " clubs cards dealt"
print "There were " + str(DiaC) + " diamonds cards dealt"
print "There were " + str(HeartC) + " heart cards dealt"
print "There were " + str(SpadeC) + " spade cards dealt"
raw_input()
I'm having trouble with this code and i wanted to see if anyone can see a problem with the code. It runs occasionally but most of the time it only inputs the print type and doesn't do anything else.
Part of my belief is that my computer may just not be able to handle the proccessing of 1000 runs through.
Reply
#2
Could you elaborate? What do you mean it "only inputs the print type"? Your computer probably isn't the problem.

Also, what exactly are you trying to do here? randrange() is probably better to use, and for that matter, you really just want to select from four possibilities (such as with random.choice()), you don't need the range checking you have going on.
Reply
#3
(Nov-17-2016, 09:48 PM)Paradoxalis Wrote: Part of my belief is that my computer may just not be able to handle the proccessing of 1000 runs through.
1000 is nothing. The var Count was over a million by the time i got to ctrl + C to kill your program.

your whole code reeks of C style. I would suggest a complete change, but to answer why you code does not work....

you pretty much created an infinite loop by not allowing the exit clause to ever execute.
Quote:
    elif Count == 1000:
        print "1000 cards have been dealt"
        break
this never executes because the var Cards is always between 0-53, and the if conditions before it checks for all these, never allowing the last elif to execute because one before it always does.

what you are looking for is to create a second if structure by doing this instead
    if Count >= 1000:
or make the condition in the while loop
Recommended Tutorials:
Reply
#4
I Wasn't too sure if I should add two cents or not.
This is for illustration purposes of other ways to create a deck:

Fluent Python is one of my favorite python books:

# attribution:
# “Fluent Python by Luciano Ramalho (O’Reilly).
#  Copyright 2015 Luciano Ramalho, 978-1-491-94600-8.
import collections
from random import shuffle

Card = collections.namedtuple('Card', ['rank', 'suit'])


class FrenchDeck(collections.MutableSequence):
    ranks = [str(n) for n in range(2, 11)] + list('JQKA')
    suits = 'spades diamonds clubs hearts'.split()

    def __init__(self):
        self._cards = [Card(rank, suit) for suit in self.suits
                       for rank in self.ranks]

    def __len__(self):
        return len(self._cards)

    def __getitem__(self, position):
        return self._cards[position]

    def __setitem__(self, position, value):  # <1>
        self._cards[position] = value

    def __delitem__(self, position):  # <2>
        del self._cards[position]

    def insert(self, position, value):  # <3>
        self._cards.insert(position, value)


def main():
    deck = FrenchDeck()
    shuffle(deck)
    for card in deck:
        print(card)


if __name__ == '__main__':
    main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Excel from SAP - dealing with formats and VBA MasterOfDestr 7 447 Feb-25-2024, 12:23 PM
Last Post: Pedroski55
  UnicodeEncodeError - Dealing with Japanese Characters fioranosnake 2 2,353 Jul-07-2022, 08:43 PM
Last Post: fioranosnake
  Having strange results from an RFID HID card reader - I'm stuck orbisnz 1 1,431 Mar-28-2022, 08:20 AM
Last Post: Larz60+
  SQL wild card use hammer 3 1,220 Jan-07-2022, 02:17 PM
Last Post: hammer
  Dealing with duplicated data in a CSV file bts001 10 11,188 Sep-06-2021, 12:11 AM
Last Post: SamHobbs
  Dealing with a .json nightmare... ideas? t4keheart 10 4,249 Jan-28-2020, 10:12 PM
Last Post: t4keheart
  Dealing with Exponential data parthi1705 11 9,582 May-30-2019, 10:16 AM
Last Post: buran
  Credit card number redacting script Drone4four 6 5,122 Jan-18-2019, 02:07 PM
Last Post: Drone4four
  Dealing with multiple context managers heras 5 4,600 Nov-16-2018, 09:01 AM
Last Post: DeaD_EyE
  Validating credit card frequency 8 4,099 Nov-05-2018, 07:36 PM
Last Post: frequency

Forum Jump:

User Panel Messages

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