Search code examples
pythonclassblackjack

Trouble with restarting a program - Python


I'm trying to make a very simple blackjack game in python and it works well. The problem is that I want to restart it when it finishes, but doesn't restart. Any ideas? Piece of code import random

class BlackjackGame():
    def __init__(self):
        self.randomNumberMachine = random.randint(4,24)
        self.randomNumberUser = random.randint(4,24)
        self.conditionalBoolean = False

    def blackjackMain(self):

        self.numberInput = int(input("Introduzca la apuesta "))

        while not self.conditionalBoolean:
            if self.randomNumberUser > 21:

                print("Desgraciadamente has perdido\nPuntos de la maquina: " + str(self.randomNumberMachine) + "\nPuntos tuyos: " + str(self.randomNumberUser))

                self.numberInput = self.numberInput / 2


                if self.numberInput <= 0:
                    print("Tienes solo 0 euros")
                else:
                    print("Tienes " + str(self.numberInput) + " euros")


                self.conditionalBoolean = True
            else:
                userChoice = int(input(("Tienes " + str(self.randomNumberUser) + "puntos, deseas:\n1)Retirarte\n2)Aumentar\n")))

                if userChoice == 1:

                    if self.randomNumberUser > self.randomNumberMachine or self.randomNumberMachine > 21:
                        print("Enhorabuena, has ganado\nPuntos de la maquina: " + str(self.randomNumberMachine) + "\nPuntos tuyos: " + str(self.randomNumberUser))
                        self.numberInput = self.numberInput * 2
                        print("Ahora tienes " + str(self.numberInput) + " euros")

                    elif self.randomNumberMachine == self.randomNumberUser:
                        print("Empate\nPuntos de la maquina: " + str(self.randomNumberMachine) + "\nPuntos tuyos: " + str(self.randomNumberUser))

                    else:


                        print("Desgraciadamente has perdido\nPuntos de la maquina: " + str(self.randomNumberMachine) + "\nPuntos tuyos: " + str(self.randomNumberUser))

                        self.numberInput = self.numberInput / 2


                        if self.numberInput <= 0:
                            print("Tienes solo 0 euros")
                        else:
                            print("Tienes " + str(self.numberInput) + " euros")


                    self.conditionalBoolean = True

                elif userChoice == 2:

                    self.aumentPoints = random.randint(2,10)
                    self.randomNumberUser = self.randomNumberUser + self.aumentPoints

        self.blackjackMain()



jugador1 = BlackjackGame()

jugador1.blackjackMain()

The self.numerInput input it keeps asking the same thing and doesn't restart the program.


Solution

  • Inside blackjackMain(self), the variable self.conditionalBoolean is only set to True. When this happens, you while condition is always False and the code inside is not executed anymore. If you want to enter the while block, you need to set the variable somewhere outside of this block.