重新启动程序时遇到问题 - Python
Trouble with restarting a program - Python
我正在尝试在 python 中制作一个非常简单的二十一点游戏,并且运行良好。问题是我想在它完成后重新启动它,但没有重新启动。有任何想法吗?
一段代码
随机导入
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()
self.numerInput 输入它一直询问同样的事情并且不重新启动程序。
在 blackjackMain(self) 内部,变量 self.conditionalBoolean 仅设置为 True。当这种情况发生时,你的 while 条件总是为 False,里面的代码就不再执行了。
如果要进入while块,需要在这个块外的某处设置变量。
我正在尝试在 python 中制作一个非常简单的二十一点游戏,并且运行良好。问题是我想在它完成后重新启动它,但没有重新启动。有任何想法吗? 一段代码 随机导入
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()
self.numerInput 输入它一直询问同样的事情并且不重新启动程序。
在 blackjackMain(self) 内部,变量 self.conditionalBoolean 仅设置为 True。当这种情况发生时,你的 while 条件总是为 False,里面的代码就不再执行了。 如果要进入while块,需要在这个块外的某处设置变量。