Pourquoi mon programme python ne fonctionne pas ?
Bonjour,
Je dois coder le jeu 'black jack'. Au moment de proposer au joueur de rejouer; la question se repose à nouveau, comme si j'étais dans une boucle infinie (alors que je n'utilise pas de
Je dois coder le jeu 'black jack'. Au moment de proposer au joueur de rejouer; la question se repose à nouveau, comme si j'étais dans une boucle infinie (alors que je n'utilise pas de
while).
import random #1 def deck(): color = ["spades", "diamonds", "hearts", "clubs"] #creation of a list of the 4 symbols deck = [] #creation of an empty list for s in color : cards = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king", "ace"] #creation of a list of the value for n in cards : c = n + " of " + s #c is the cards with a value and a symbole deck.append(c) #add c to the empty list = deck of cards return deck #2 def valueCard(card) : nb = card[0] #take only the first character of the string if nb >= '2' and nb <= '9' : value = int(card[0]) elif nb == '1' or nb == 'j' or nb == 'q' or nb == 'k' : value = 10 else : value = int(input("which value chosen ? ( 1 or 11 )")) #ask the user to chose the value wanted return value #3 def initstack(n) : # n is the number of player chose by the user p = [] #creation of an empty list for i in range(0, n + 1): # add n-deck of card to the empty list y = deck() # y is a deck of card p.extend(y) # add y to p random.shuffle(y) return p # p is list of the n-deck shuffled #4 def drawCard(p, x=1): # x is the number of cards dc = [] #creation of a empty list for i in range (0, x) : card = p[0] #card is the first element of the srting dc.append(card) #add to the list dc p.remove(card) #remove card from the list of n-deck return dc #dc is the list of thge x-cards choosen #1 def initPlayers(n): joueurs = [] for i in range (0, n) : name = input("name of the player ?") joueurs.insert(i, name) return joueurs #2 def initScores(joueurs, v=0): score = {} for i in joueurs : score[i] = v return score #3 def firstTurn(players): scores =initScores(players, 0) p = initstack(len(players)) for i in range(0, len(players)) : cards = drawCard(p, 2) print(cards) score = 0 name = players[i] for j in range(0, 2) : v = valueCard(cards[j]) score = score + v scores[name] = score return scores #4 def winner(scores) : wins = 0 for key in scores: if scores[key] > wins and wins <= 21 : wins = scores[key] name = key return name, wins def Continue() : c = input("do you want to continue ? yes/no ") while c !='yes' or c !='no' : c = input("do you want to continue ? yes/no ") if c =='yes' : return True elif c =='no' : return False def playerTurn(turn, namep, scores, p, x) : print('BLACK JACK') print('turn number:', turn) print('the player is:', namep) print('current total of point:', scores[namep]) savescore={} if Continue()==True : newcard= drawCard(p, x) v = valueCard(newcard) print('the value of the card is:',v) scores[namep]= scores[namep] + v if scores[namep] == 21 : print('YOU ARE THE WINNER !!!!') elif scores[namep] > 21 : print('YOU LOOSE',) savescore[namep]= scores[namep] del scores[namep] elif Continue()==False : savescore[namep]= scores[namep] del scores[namep] print('END OF YOUR GAME') return scores[namep] def gameTurn(joueurs, scores, p, x) : scores2=dict(scores) #copy of the dictionary scores turn = 1 for i in joueurs : #chose the player if i in scores : #verify if the player is still in play scores2 = scores2[i] + (playerTurn(turn, i, scores, p, x)) #upload the score of the player in the dictionary scores2 if scores2[i] == 21: # if the score of the player is 21, the game stop return scores2 elif not i in scores : del scores2[i] #if the player is not in the dictionary scores it will be removed of the dictionary scores2, this signify the player decide to don't continuate or the player loose turn=turn+1 return scores2 #return the new dictionary with the new values of the players still in play def gameOver(scores2): flag = False if (21 in scores2.value()) or (not scores2) : #if 21 is a value in scores2 or if scores2 is empty flag = True # the game is finished return flag def findkey(scores2): for i in scores2: if scores2[i] == 21: winner = i return winner def completeGame(joueurs, scores, p, x): nwin = initScores(joueurs, v=0) gameround = gameTurn(joueurs, scores, p, x) gO = gameOver(gameround) while gO == True : winner = findkey(gameround) nwin[winner] = nwin[winner]+1 return nwin ..................................................main program ............................................................ n = int(input('nb of players ?')) p = initstack(n) playagain = 'yes' nbgame = 0 while playagain == 'yes' : x = int(input('nb of cards ?')) dc = drawCard(p, x) joueurs = initPlayers(n) score = initScores(joueurs, 0) scores = firstTurn(joueurs) BJgame= completeGame(joueurs, scores, p, x) print(BJgame) nbgame = nbgame + 1 playagain = imput('Do you want to play an other time? (yes/no)') overallwinner = winner(BJgame) print('you played:', nbgame, 'the overall winner is:', overallwinner)
Configuration: Macintosh / Chrome 96.0.4664.55
A voir également:
- Pourquoi mon programme python ne fonctionne pas ?
- Citizen code python avis - Accueil - Outils
- Programme demarrage windows - Guide
- Cette action ne peut pas être réalisée car le fichier est ouvert dans un autre programme - Guide
- Comment fermer un programme qui ne répond pas - Guide
- Message programmé iphone - Guide
2 réponses
Bonjour
En Python, l’indentation est primordiale, or par défaut le site ne la conserve pas.
Pour la conserver il faut utiliser les balises de code. Voir ce petit tuto https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
On pourra commencer à essayer de t’aider quand tu auras reposté correctement ton code.
En Python, l’indentation est primordiale, or par défaut le site ne la conserve pas.
Pour la conserver il faut utiliser les balises de code. Voir ce petit tuto https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
On pourra commencer à essayer de t’aider quand tu auras reposté correctement ton code.
Bonjour,
Il y a plusieurs erreurs :
Tu devrais peut-être afficher la valeur de
Bonne chance
Il y a plusieurs erreurs :
-
imput
au lieu deinput
; -
...........................main program ......................
devrait être commenté avec un#
. - Enfin, le passage suivant est bien dans une boucle
while
:
playagain = input('Do you want to play an other time? (yes/no)')
Tu devrais peut-être afficher la valeur de
playagainpour comprendre pourquoi la variable ne change pas de valeur.
Bonne chance