简体   繁体   English

重新启动程序时遇到问题-Python

[英]Trouble with restarting a program - Python

I'm trying to make a very simple blackjack game in python and it works well. 我正在尝试使用python创建一个非常简单的二十一点游戏,并且效果很好。 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. self.numerInput输入会不断询问相同的问题,并且不会重新启动程序。

Inside blackjackMain(self), the variable self.conditionalBoolean is only set to True. 在blackjackMain(self)内部,变量self.conditionalBoolean仅设置为True。 When this happens, you while condition is always False and the code inside is not executed anymore. 发生这种情况时,您的条件始终为False,并且不再执行内部代码。 If you want to enter the while block, you need to set the variable somewhere outside of this block. 如果要输入while块,则需要在该块之外的某个位置设置变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM