Python Forum
"Card Dealing" Python code 2.7 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: "Card Dealing" Python code 2.7 (/thread-983.html)



"Card Dealing" Python code 2.7 - Paradoxalis - Nov-17-2016

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.


RE: "Card Dealing" Python code 2.7 - micseydel - Nov-17-2016

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.


RE: "Card Dealing" Python code 2.7 - metulburr - Nov-17-2016

(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


RE: "Card Dealing" Python code 2.7 - Larz60+ - Nov-17-2016

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()