我试图在Python上制作一个骰子21游戏(如果你需要,请查看,这里输入的时间太长)。它还没有完成,但是现在我正在修复我所犯的任何错误。我遇到了一些不会关闭的while循环问题。玩家选择坚持使用diceroll功能后,应将playeraddrolls设置为False并退出while循环,进入计算机功能。然而,它只是循环回来。需要立即帮助,因为这是一个周一的学校项目,在我仍然需要完成代码之后。如果你能指出我稍后会遇到的任何其他错误,以及如何修复它们,它也会有很大的帮助。
import random
stick=0
winner=[""]
def diceroll(addedrolls,stick,playagain,playagain1or2,playeraddedrolls,computeraddedrolls,playeraddrolls):
while playeraddedrolls<21 or playeraddrolls is True:
stick=0
die1=random.randint(1,6)
die2=random.randint(1,6)
print("You rolled ",die1,"and ",die2,".")
playeraddedrolls=die1+die2
if playeraddedrolls>21:
print("You rolled over 21. Computer wins by default!")
computeraddedrolls(playeraddedrolls,playagain,playagain1or2,computeraddedrolls)
else:
while stick>2 or stick<1:
stick=int(input("Press 1 to stick or 2 to roll again. "))
if stick==1:
print("You chose to stick at", playeraddedrolls,". The computer will now roll.")
playeraddrolls=False
computeroll(playeraddedrolls,playagain,playagain1or2,computeraddedrolls)
elif stick==2:
print("You chose to roll again. Producing numbers now.")
else:
print("I'm sorry, that's not a valid command.")
def computeroll(playeraddedrolls,playagain,playagain1or2,computeraddedrolls):
while computeroll<17:
die3=random.randint(1,6)
die4=random.randint(1,6)
print("The comoputer rolled ",die3,"and ",die4,".")
computeraddedrolls=die3+die4
if playeraddedrolls>21:
winningtally(playeraddedrolls,computeraddedrolls,playagain,playagain1or2)
else:
if computeraddedrolls<17:
print("The computer chose to roll again!")
elif computeraddedrolls>21:
print("The computer rolled over 21, you win by default!")
winningtally(playeraddedrolls,computeraddedrolls,playagain,playagain1or2)
else:
print("Overall, the computer scored ", computeraddedrolls,".")
winningtally(playeraddedrolls,computeraddedrolls,playagain,playagain1or2)
def winningtally(PAR,CAR,playagain,playagain1or2):
if playeraddedrolls>21 or computeraddedrolls>playeraddedrolls:
print("I have added the computers win to the tally. Here is the new set of wins:")
append(computer)
print(winner)
playagain(PAR,CAR,playagain,playagain1or2)
elif computeraddedrolls>21 or playeraddedrolls>computeraddedrolls:
print("I have added your win to the tally. Here is the new set of wins:")
append(player)
print(winner)
playagain(PAR,CAR,playagain,playagain1or2)
def playagain(PAR,CAR,playagain,playagain1or2):
while playagain1or2<1 or playagain1or2>2:
playagain1or2=int(input("Press 1 to play again, or 2 to view the final result."))
if playagain1or2==1:
print("Okay, rerunning...")
return
elif playagain1or2==2:
computerwins=(winner).count(computer)
playerwins=(winner).count(player)
if computerwins>playerwins:
print("Sorry, the computer won. Better luck next time!")
else:
print("Congratulations, you won! Thank you for playing!")
else:
print("I'm sorry, ",playagain1or2," is not a valid command.")
playeraddrolls=True
playeraddedrolls=2
computeraddedrolls=2
playagain1or2=0
playagain=True
while playagain==True:
stick=0
addedrolls=3
diceroll(addedrolls,stick,playagain,playagain1or2,playeraddedrolls,computeraddedrolls,playeraddrolls)
答案 0 :(得分:0)
假设您的代码按表达式运行(需要检查的代码很多),那么您的问题就是False < 21 == True
。
这是您的while
条件:
while playeraddedrolls<21 or playeraddrolls is True:
请记住or
短路。只需要有一件事是真实的,因为整个x or y or z or...
字符串在逻辑上是真的,所以一旦检查的第一件事情为真,or
就会停止查看。
由于您要设置playeraddedrolls = False
以摆脱此循环,因此检查变为False < 21
,这是真的并且短路。
您可以在那里明确添加playeraddedrolls = False
,而不是隐式设置break
和 。但是,不建议这样做,因为break
语句很容易被掩埋,因此很难调试。
或许更好的方法是将while条件更改为:
while 0 < playeraddedrolls < 21:
这允许您为所需的隐式中断设置playeraddedrolls = -1
。
正如我在评论中所解释的那样,布尔值是整数的子类,因为可以将True和False视为数字0和1的特殊情况。这可以让你对布尔值做一些可能令人惊讶的数字事情。
>>> True + False
1
>>> True - False
1
>>> True * False
0
>>> True % False
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> False % True
0
你可以看到上面所有数值运算符只是将布尔值强制为0和1,并且运行得非常愉快。
答案 1 :(得分:0)
看着它,虽然我不太有经验,但我认为问题可能出在缩进方面。添加缩进可以使所有内容正确对齐,使其正常工作。
def winningtally(PAR,CAR,playagain,playagain1or2):
if playeraddedrolls>21 or computeraddedrolls>playeraddedrolls:
#everything else would be the same, just indented