2015-10-28 270 views
0

我想在Python上做一個骰子21遊戲(如果你需要查找,在這裏輸入的時間太長)。它還沒有完成,但現在我正在經歷並修復我犯的任何錯誤。我有一些while循環不會關閉的問題。在玩家選擇堅持diceroll函數之後,它應該將playeraddrolls設置爲False並退出while循環,進入到computeroll函數中。但是,它只是循環回來。需要立即的幫助,因爲這是週一的一個學校項目,在我仍然需要完成代碼之後。如果您能指出我稍後會遇到的任何其他錯誤,以及如何解決這些錯誤,這也會有所幫助。Python 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) 
+1

打開Python解釋器並鍵入False <21'。結果將解釋您的問題。其解釋是,布爾運算符「False」和「True」只是整數的特殊情況 - 0和1. –

+0

將變量重新定義爲* integer *或* True可能不是一個好主意/假。使用一個單獨的變量。 –

回答

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 0 < playeraddedrolls < 21: 

這允許您設置playeraddedrolls = -1所希望的隱含突破。

但是爲什麼Python會這樣做?

正如我的評論所述,布爾值是整數的一個子類,因爲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,功能相當愉快。