2014-04-15 3 views
0

그래서, 나는이 포럼의 사용자들로부터 도움을 받아 웹을 둘러 보는 것으로부터 프로그램 시뮬레이터를 연구하고있다. 나는 대부분 그것을 끝낼 수있었습니다. 적어도 나는 게임의 논리를 가지고 있다고 생각한다.While loop 영원히 반복하다. Python

지난 번 실행했을 때 실행 중이었지만 지금은 while 루프가 영원히 실행되기 때문에 뭔가를 인식하지 못하고 변경해야합니다. 프로그램에서 주사위는 사용자의 개입없이 영원히 계속 굴러 가고 있습니다.

누군가 코드를 볼 수 있고 코드에 문제가 있는지 확인할 수 있습니까? 나는 몇 시간 동안이 일을하고 있었고, 어디를 가는지 알지 못했다.

나는 결코 다시는 도박을하지 않을 것이다! :/

from Dice import PairOfDice 
print("Now Let's play with two dice!") 
# 
def MainDouble(): 
    bdice = PairOfDice() 
    doubleDiceRoll = '' 
    global myPoint #declare global variable 
    input("Press Enter to Roll the Dice once") 

    while doubleDiceRoll == '': #Roll the dice (both die objects) 
     bdice.toss() 
     print ('The first die reads.. ' + str(bdice.getValue1()) + '\n') 
     print ('The second die reads.. ' + str(bdice.getValue2()) + '\n') 

     Val = bdice.getTotal() 

##Beginning of conditional blocks## 
    if Val == 7 or Val == 11: 
     gamestatus = "WON" 


    elif Val == 2 or Val == 3 or Val == 12: 
     gamestatus = "LOST" 

    if Val != 7 and bdice.getTotal() != 11 and Val != 2 and Val != 3 and Val != 12: 
     gamestatus = "CONTINUE" 
      # 
     myPoint = Val 
     print("The Point is now " + myPoint + "/n") #display the user's point value 
     global pSum 
     pSum = 0 


    #The point 

    while gamestatus == "CONTINUE": #Checking the point 
      global point 
      point = myPoint 
      pSum = MainDouble() 

      if pSum == myPoint: 
      gamestatus == "WON" 
      elif pSum == 7: 
      gamestatus = "LOST" 
      if gamestatus == "WON": 
      print("Winner!") 
      else: 
      print("Sorry, Seven out!") 
      print ("Roll Again?") 

    doubleDiceRoll = input("Roll Again?") 

MainDouble() 
+0

스크립트의 마지막에'doubleDiceRoll = input ("Roll Again?")'이 있지만'while doubleDiceRoll == '''안에 있지 않습니다. 아무것도 변경하지 않고 항상 참입니다. – mhlester

답변

5

이 블록 :

while doubleDiceRoll == '': #Roll the dice (both die objects) 
    bdice.toss() 
    print ('The first die reads.. ' + str(bdice.getValue1()) + '\n') 
    print ('The second die reads.. ' + str(bdice.getValue2()) + '\n') 

    Val = bdice.getTotal() 

doubleDiceRoll

'' 변경되지 않습니다 때문에,이 루프가 영원히 실행됩니다.

##Beginning of conditional blocks##

이 라인 아래 모든이 밖에이 블록의 끝 (!하지만 여전히 내부)에서, 당신은

doubleDiceRoll = raw_input("Roll Again?") #Edit thanks to @Adam Smith 
+0

iirc'doubleDiceRoll'은 "다시 재생 하시겠습니까?"라는 메시지를 사용자에게 표시합니다. 나는 이전에 올린 글 (내가 받아 들인 대답을 가지고 있음)을 다시 봐야만 할 것이다. 편집 : 옙 그의 원래'while' 루프의 끝에'doubleDiceRoll = raw_input ("Roll Again?")'줄이 있습니다. 모든 것이'while' 블록 안에 있어야하는 것처럼 보입니다. –

+1

좋아, 어느 쪽의 방법 으로든 'doubleDiceRoll'을 업데이트해야합니다. 나는 나의 대답을 업데이트했다. – wflynny

+0

감사 빌과 mhelster. 그 문제를 해결하려했지만 while 문을 false로 변경하면 코드가 멈 춥니 다. 어떤 아이디어입니까? – user3462223

2

같은 것을해야 난 후에 당신이 당신의 들여 쓰기를 엉망 생각 하지만 while 루프에 있어야합니다.