card dealer

Alister alister.ware at ntlworld.com
Fri Sep 27 07:41:55 EDT 2013


On Fri, 27 Sep 2013 03:24:28 -0700, markotaht wrote:

> from random import *
> from math import floor
> 
> 
> kaarte_alles = 52 kaart_tõmmatud = [False for i in range(52)]
> 
> 
> mast = ["ärtu", "ruutu", "poti", "risti"]
> aste = ["äss", "kaks", "kolm", "neli","viis", "kuus", \
>         "seitse", "kaheksa", "üheksa", "kümme", "soldat",\
>         "emand", "kuningas"]
> 
> def tõmba_kaart():
>     global kaarte_alles if kaarte_alles == 0:
>         print("Segan pakki")
>         kaarte_alles = 52 for i in range(52):
>             kaart_tõmmatud[i] = False
>     kaarte_alles -= 1 n = random(kaarte_alles)
>     kaart = vali_järgmine_vaba(n)
>     m = kaart % 13 a = kaart / 13 print(mast[int(a)] + " " +
>     aste[int(m)])
> 
> def vali_järgmine_vaba(n):
>     i = -1
> 
>     while(n  > 0):
>         n -= 1 i = i + 1 while kaart_tõmmatud[i]:
>             i = i + 1
>     kaart_tõmmatud[i] = True return i
> 
> def random(n):
>     return randint(0, n)
> 
> while True:
>     n = int(input("Mitu kaarti tõmmata(0 et väljuda): "))
>     if(n==0):
>         break
> 
>     for i in range(n):
>         tõmba_kaart()
> 
> 
> HI im trying to make a card dealer, but this code doesent work
> correctly. It deasl cards, but the cards to repeat. when i type in 52
> ten i might get 3 the same cards. This is a translation from my c++
> code. In c++ it works correctly.


It looks like you a simply selecting a random entry in the deck 
i would suggest you try the following

1) build the deck as a list (i think you are already doing this)
2) shuffle the list (random.shuffle would be good here)
3) pop the cards of the list as required

this resemble the real world & should be easy to break down into various 
classes & expand for multiple decks if req.


-- 
Passionate hatred can give meaning and purpose to an empty life.
		-- Eric Hoffer



More information about the Python-list mailing list