2
\$\begingroup\$

Is there any way to reduce this code? It repeats itself, but I cannot find any way to reduce it because it is only repeating inside one elif. Sorry I'm a beginner my code is quite bad It repeats here:

           onze(joueurs)
           print("joueur", a + 1, "a", joueurs[a])
           time.sleep(1)
           while sum(joueurs[a]) < 21 or 11 in joueurs[a]: 

The code :

while t:
    input("Appuyez sur entrer pour continuer...")
    for a in range(nb):
        while sum(joueurs[a]) < 21 or 11 in joueurs[a]:
            time.sleep(0.5)
            print("main du joueur", a + 1, ":", joueurs[a], "Total :", sum(joueurs[a]))
            if sum(joueurs[a]) > 21 and 11 in joueurs[a]:
                onze(joueurs)
                print("joueur", a + 1, "a", joueurs[a])
            ask = input("Joueur {} voulez vous une carte ?(oui/non)".format(a + 1))
            if ask == "oui":
                askoui(joueurs)
            else:
                asknon()
                t = False
                break
        if sum(joueurs[a]) == 21:
            print("joueur", a + 1, "a BLACKJACK")
            time.sleep(1)
        elif sum(joueurs[a]) > 21 and 11 in joueurs[a]:
            onze(joueurs)
            print("joueur", a + 1, "a", joueurs[a])
            time.sleep(1)
            while sum(joueurs[a]) < 21 or 11 in joueurs[a]: 
                time.sleep(0.5)
                print("main du joueur", a + 1, ":", joueurs[a], "Total :", sum(joueurs[a]))
                if sum(joueurs[a]) > 21 and 11 in joueurs[a]:
                    onze(joueurs)
                    print("joueur", a + 1, "a", joueurs[a])
                ask = input("Joueur {} voulez vous une carte ?(oui/non)".format(a + 1))
                if ask == "oui":
                    askoui(joueurs)
                else:
                    asknon()
                    t = False
                    break
            elif sum(joueurs[a]) > 21:
                perdu(joueurs)
                t = False
        elif sum(joueurs[a]) > 21:
            perdu(joueurs)
            t = False
\$\endgroup\$
2
  • 1
    \$\begingroup\$ you can always put code in function and use this function. But for two lines it can be better to keep current version. \$\endgroup\$ – furas May 7 at 7:20
  • \$\begingroup\$ Could you share your whole code? That would make it easier to give advice. \$\endgroup\$ – joostblack May 7 at 14:40
1
\$\begingroup\$

It's "Pythonic" to iterate through items, so assuming that len(joueurs) == nb, your main for loop can be:

    for a, joueur in enumerate(joueurs):

which means all your joueurs[a] become joueur. I'm quite surprised that onze, askoui and perdu functions take the whole joueurs list, not just the current player. How does that work?

I'd guess that the check for possible onze action, which I take it is converting an ace down from 11 points to 1, could more comfortably fit within the askoui function when our current player gets another card, non? Then this main routine could just check against 21, or perhaps even better the askoui function could return a status value.

Difficult to be more specific without getting into some of these auxiliary functions. Here's my best guess at a reduced code that I think should do the same as you presently have, plus some outstanding issues:

while t:
    input("Appuyez sur entrer pour continuer...")
    for ix, jr in enumerate(joueurs):
        jID = ix+1
        while sum(jr) < 21:
            time.sleep(0.5)
            print("main du joueur", jID, ":", jr, "Total :", sum(jr))
            ask = input("Joueur {} voulez vous une carte ?(oui/non)".format(jID))
            if ask == "oui":
                askoui(joueurs) # why all joueurs?
                if sum(jr) > 21 and 11 in jr:
                    onze(joueurs) # why all joueurs?
            else:
                asknon()
                t = False # immediate halt to play for all players?
                break
        if sum(jr) == 21:
            print("joueur", jID, "a BLACKJACK")
            time.sleep(1) # then play continues
        elif sum(jr) > 21:
            perdu(joueurs) # why all joueurs?
            t = False
        # play continues without message if sum(jr)<21 ?
\$\endgroup\$
1
  • \$\begingroup\$ Thanks for your time, I couldn't post all my code because Stackexchange lock the amount of code that can be posted. I understood your idea and I think I could make that work. I'll get back to you as soon as I try ! thanks for your answer \$\endgroup\$ – Tassanix May 15 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.