IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

Probleme code source python, poker


Sujet :

Python

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Maitre shaolin, pro du full contact
    Inscrit en
    Octobre 2012
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Maitre shaolin, pro du full contact
    Secteur : Service public

    Informations forums :
    Inscription : Octobre 2012
    Messages : 4
    Points : 1
    Points
    1
    Par défaut Probleme code source python, poker
    Yo, on a un code source pour calculer les probas pour le poker, cependant il y'a quelques erreurs dans le code (que l'on a prit sur code sources) pourriez vous nous indiquez lesquelles, et comment y remedier ? (on utilise python 2.7.3)
    Merci d'avance !
    PS : on a modifier 2-3 petits trucs de code

    from random import *
    import time
    tps1=time.clock()
    def hand(cartes):
    #The aim of this function is to determine the best 5 card hand
    #out of a set of 7 cards.
    #Pair
    main = cartes[:]
    sortie = 0
    exit = 0
    a = 10000
    for i in range(7):
    if (int(cartes[i]/100)==2):
    a += 10
    elif (int(cartes[i]/100)==3):
    a += 100
    elif (int(cartes[i]/100)==4):
    a += 1000
    else:
    a += 1
    # print a
    a = str(a)
    if (a[1]>='5' or a[2]>='5' or a[3]>='5' or a[4]>='5'):
    # The hand present at least a flush :
    if (a[1]>='5'):
    for i in range(7):
    if (int(cartes[i]/100)!=4):
    main.remove(cartes[i])
    if (len(main)==5):
    break
    elif (a[2]>='5'):
    for i in range(7):
    if (int(cartes[i]/100)!=3):
    main.remove(cartes[i])
    if (len(main)==5):
    break
    elif (a[3]>='5'):
    for i in range(7):
    if (int(cartes[i]/100)!=2):
    main.remove(cartes[i])
    if (len(main)==5):
    break
    elif (a[4]>='5'):
    for i in range(7):
    if (int(cartes[i]/100)!=1):
    main.remove(cartes[i])
    if (len(main)==5):
    break
    for i in range(len(main)):
    main[i]=main[i]%100
    main.sort()
    main.reverse()
    main2 = list(set(main))
    main2.reverse()
    if (len(main2)>=5):
    for i in range(len(main2)-4):
    if (main2[i]==main2[i+4]+4):
    # print 'suited flush'
    sortie = (main2[i])*10**20
    exit = 1
    if (exit==0):
    # print main
    if (main2[0]==13 and main2[-1]==1):
    if (main2[-4]==main2[-1]+3):
    # print 'little suite'
    sortie = (main2[-4])*10**12
    exit=1
    if (exit==0):
    for i in range(5):
    # print 'flush'
    sortie += (main[i])*10**(2*(4-i))
    sortie = sortie*10**6
    else:
    # 2oaK // 3oaK // suite // Full // 4oaK
    for i in range(len(main)):
    main[i]=main[i]%100
    main.sort()
    main.reverse()
    main2 = list(set(main))
    main2.reverse()
    if (len(main2)>=5):
    for i in range(len(main2)-4):
    if (main2[i]==main2[i+4]+4):
    # print 'suited'
    sortie = (main2[i])*10**12
    exit=1
    if (exit==0):
    # print main
    if (main2[0]==13 and main2[-1]==1):
    if (main2[-4]==main2[-1]+3):
    # print 'little suite'
    sortie = (main2[-4])*10**12
    exit=1
    for i in range(len(main)-3):
    if (exit==0):
    if (main[i]==main[i+3]):
    # print '4 of a kind'
    sortie = main[i]
    main.remove(main[i])
    main.remove(main[i])
    main.remove(main[i])
    main.remove(main[i])
    sortie = sortie*10**18 + main[0]
    exit=1
    for i in range(len(main)-2):
    if (exit==0):
    if (main[i]==main[i+2]):
    sortie = main[i]
    main.remove(main[i])
    main.remove(main[i])
    main.remove(main[i])
    for j in range(len(main)-1):
    if (main[j]==main[j+1]):
    # print 'full'
    sortie = sortie*10**16 + main[j]
    exit=1
    if (exit==0):
    # print '3 of a kind'
    sortie = sortie*10**10+main[0]*100+main[1]
    exit=1
    for i in range(len(main)-1):
    if (exit==0):
    if (main[i]==main[i+1]):
    sortie = main[i]
    main.remove(main[i])
    main.remove(main[i])
    for j in range(len(main)-1):
    if (main[j]==main[j+1]):
    # print '2 pairs'
    sortie = sortie*10**8 + main[j]*10**6
    main.remove(main[j])
    main.remove(main[j])
    sortie += main[0]
    exit=1
    break
    if (exit==0):
    # print '2 of a kind'
    sortie = sortie*10**6
    sortie += (main[0])*10**4+(main[1])*100+main[2]
    exit=1
    if (exit==0):
    # print 'nothing'
    for i in range(5):
    sortie += main[i]
    return sortie
    Clubs = ['C2','C3','C4','C5','C6','C7','C8','C9','CT','CJ','CQ','CK','CA']
    Diamonds = ['D2','D3','D4','D5','D6','D7','D8','D9','DT','DJ','DQ','DK','DA']
    Hearts = ['H2','H3','H4','H5','H6','H7','H8','H9','HT','HJ','HQ','HK','HA']
    Spades = ['S2','S3','S4','S5','S6','S7','S8','S9','ST','SJ','SQ','SK','SA']
    Jeu = Clubs + Diamonds + Hearts + Spades
    nplayers = input('Number of players ?')
    # nplayers = 9
    card1=raw_input('\n Please enter your first card :')
    card2=raw_input('Please enter your second card :')
    myhand=[[card1,card2]]
    # myhand = [['CA','SA']]
    N = 5000
    gain = zeros(nplayers)
    for iter in range(N):
    hands=[[0,0]]
    # print iter
    # Jeu_1 : cards that might stay in the game
    # Jeu_2 : index of theses cards
    Jeu_1 = Jeu[:]
    Jeu_2 = range(52)
    for i in range(13):
    for j in range(4):
    Jeu_2[j*13+i] = 100*(j+1)+(i+1)
    #
    # print hands
    # translation into card index :
    for i in range(2):
    if myhand[0][i][0]=='C':
    n = Clubs.index(myhand[0][i])
    carte = 100+(n+1)
    elif myhand[0][i][0]=='D':
    n = Diamonds.index(myhand[0][i])
    carte = 200+(n+1)
    elif myhand[0][i][0]=='H':
    n = Hearts.index(myhand[0][i])
    carte = 300+(n+1)
    elif myhand[0][i][0]=='S':
    n = Spades.index(myhand[0][i])
    carte = 400+(n+1)
    hands[0][i] = carte
    #
    Jeu_1.remove(Jeu_1[Jeu_2.index(hands[0][0])])
    Jeu_2.remove(hands[0][0])
    Jeu_1.remove(Jeu_1[Jeu_2.index(hands[0][1])])
    Jeu_2.remove(hands[0][1])
    #
    #
    # first round :
    #--------------
    for n in range(nplayers-1):
    cards = [0,0]
    for i in range(2):
    rd = rand(1)
    tirage=rd*(len(Jeu_2)-1)
    # print n,i,int(tirage)
    cards[i]=Jeu_2[int(tirage)]
    Jeu_1.remove(Jeu_1[Jeu_2.index(cards[i])])
    Jeu_2.remove(cards[i])
    hands += [cards]
    #
    cards=[]
    for i in range(5):
    rd = rand(1)
    tirage=rd*(len(Jeu_2)-1)
    cards += [Jeu_2[int(tirage)]]
    Jeu_1.remove(Jeu_1[Jeu_2.index(cards[i])])
    Jeu_2.remove(cards[i])
    #
    scores=[]
    for n in range(nplayers):
    cartes = hands[n] + cards
    # print cartes
    scores += [hand(cartes)]
    # print scores
    gain[scores.index(max(scores))] += 1
    gain = gain/float(N)
    print''
    print '\tRESULTS with hand :',myhand[0][0],myhand[0][1]
    print '*******************************************'
    for i in range(len(gain)):
    if (i==0):
    print 'you','\t\t-->\t',gain[i]*100,'\t%'
    else:
    print 'player',i+1,'\t-->\t',gain[i]*100,'\t%'
    print '*******************************************'
    print 'error : ',100*(max(gain[1:])-min(gain[1:])),'%'

    tps2=time.clock()
    print 'execution time :',tps2-tps1,'s'

  2. #2
    Membre habitué

    Profil pro
    Inscrit en
    Juin 2007
    Messages
    211
    Détails du profil
    Informations personnelles :
    Âge : 74
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2007
    Messages : 211
    Points : 168
    Points
    168
    Billets dans le blog
    1
    Par défaut C'est quoi, ça ?
    Bonjour petard1994,
    Tu ne pourrais pas utiliser les balises qu'il faut pour écrire du code et aussi placer des indentations qui permettent de mieux lire le code proposé ? (Voir charte du site)

    Le code, comme tu l'as mis là est proprement illisible.

    A+

  3. #3
    Nouveau Candidat au Club
    Homme Profil pro
    Maitre shaolin, pro du full contact
    Inscrit en
    Octobre 2012
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Maitre shaolin, pro du full contact
    Secteur : Service public

    Informations forums :
    Inscription : Octobre 2012
    Messages : 4
    Points : 1
    Points
    1
    Par défaut
    Quand j'ai fais copier/coller l'indentation ne s'est pas mise, je viens de le remarquer, je veux juste savoir les erreurs, merci .

  4. #4
    Membre expérimenté Avatar de plxpy
    Homme Profil pro
    Ingénieur géographe
    Inscrit en
    Janvier 2009
    Messages
    792
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 59
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur géographe
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Janvier 2009
    Messages : 792
    Points : 1 481
    Points
    1 481
    Par défaut
    Citation Envoyé par petard1994
    je veux juste savoir les erreurs
    peut-être, mais ton code n'est pas plus lisible pour autant !!!

    L'indentation est partie intégrante de Python. Ce n'est pas juste une bonne habitude. Si toutes les instructions sont alignées à gauche, on ne peut rien en faire dès que tu fais : un test, une boucle, ...
    "La simplicité ne précède pas la complexité, elle la suit." - Alan J. Perlis
    DVP ? Pensez aux cours et tutos, ainsi qu'à la FAQ !

  5. #5
    Responsable Arduino et Systèmes Embarqués


    Avatar de f-leb
    Homme Profil pro
    Enseignant
    Inscrit en
    Janvier 2009
    Messages
    12 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Janvier 2009
    Messages : 12 617
    Points : 56 722
    Points
    56 722
    Billets dans le blog
    40
    Par défaut
    salut,

    pour les balises code, voir ici.

    et en Python, l'indentation fait partie de la syntaxe comme tu dois le savoir. Un petit effort de mise en forme donc.

    Et puis mets nous sur la voie, que fait le programme, quelles sont les erreurs rencontrées, quelle ligne etc...

  6. #6
    Nouveau Candidat au Club
    Homme Profil pro
    Maitre shaolin, pro du full contact
    Inscrit en
    Octobre 2012
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Maitre shaolin, pro du full contact
    Secteur : Service public

    Informations forums :
    Inscription : Octobre 2012
    Messages : 4
    Points : 1
    Points
    1
    Par défaut
    Encore désolé pour l'indentation, je sais que la elle n'est pas bonne mais c'est mon copier/coller qui n'a pas fonctionner, sur mon prog évidemment que l'indentation est faite. Quand je lance mon programme j'entre le nombre de joueurs, puis ma première et ma deuxième carte et c'est la que le programme plante et me sors : "NameError: name 'zeros' is not defined" le problème se trouve ligne 158 ! La ligne 158 étant "gain = zeros(nplayers)", il doit donc y avoir un problème avec le "zeros(nplayer)" mais lequel ? ...

    Merci bien .

  7. #7
    Expert éminent
    Avatar de fred1599
    Homme Profil pro
    Lead Dev Python
    Inscrit en
    Juillet 2006
    Messages
    3 823
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Lead Dev Python
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Juillet 2006
    Messages : 3 823
    Points : 7 119
    Points
    7 119
    Par défaut
    Pour zeros, c'est simple, votre fonction n'existe pas, l'interpréteur vous envoi donc un NameError
    Celui qui trouve sans chercher est celui qui a longtemps cherché sans trouver.(Bachelard)
    La connaissance s'acquiert par l'expérience, tout le reste n'est que de l'information.(Einstein)

  8. #8
    Nouveau Candidat au Club
    Homme Profil pro
    Maitre shaolin, pro du full contact
    Inscrit en
    Octobre 2012
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Maitre shaolin, pro du full contact
    Secteur : Service public

    Informations forums :
    Inscription : Octobre 2012
    Messages : 4
    Points : 1
    Points
    1
    Par défaut
    Oui j'avais compris, le "NameError" m'a mit sur la piste ahah !
    Mais comment y remedier ?

    Merci

  9. #9
    Expert éminent
    Avatar de fred1599
    Homme Profil pro
    Lead Dev Python
    Inscrit en
    Juillet 2006
    Messages
    3 823
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Lead Dev Python
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Juillet 2006
    Messages : 3 823
    Points : 7 119
    Points
    7 119
    Par défaut
    Eh bien pour cela il faudrait savoir ce que fais cette fonction zeros et la créer...
    Celui qui trouve sans chercher est celui qui a longtemps cherché sans trouver.(Bachelard)
    La connaissance s'acquiert par l'expérience, tout le reste n'est que de l'information.(Einstein)

Discussions similaires

  1. code source Python 2.7
    Par ladère dans le forum Général Python
    Réponses: 4
    Dernier message: 02/05/2014, 15h19
  2. Réponses: 11
    Dernier message: 04/03/2013, 10h52
  3. [CKEditor] FCKeditor => probleme code source insertion image
    Par kakahn dans le forum Bibliothèques & Frameworks
    Réponses: 1
    Dernier message: 04/05/2007, 11h57
  4. enregistrer/lancer un code source python
    Par laulau69 dans le forum Général Python
    Réponses: 7
    Dernier message: 21/01/2006, 10h05

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo