2015-02-02 6 views
0

(의도적으로) 루프가 의도 한대로 작동하지 않습니다. 다른 모든 것이 의도 한대로 작동한다고 가정하면, 두 번째 while 루프는 (그레이더에 ​​따라) raw_input을 필요한 것보다 더 많이 호출합니다.루핑 오류 (숙제)

코드는 단어 게임을합니다. 손을 짚거나 재생하거나 종료 할 수 있습니다. 두 번째 루프는 손이나 컴퓨터를 움직일 지 판단합니다.

모든 호출 된 함수는 올바르게 작동하지만 루프는 내가 말했듯이 raw_input을 너무 많이 호출하고 있습니다.

죄송합니다. 다른 문제가 많으면 코딩하기가 비교적 쉽습니다.

userInput = '' 
playInput = '' 
hasPlayed = False 

# while still playing, e exits 
while userInput != 'e': 

    userInput = raw_input("Enter n to deal a new hand, r to replay the last hand, or e to end game: ").lower() 

    if userInput not in 'nre': 

     print("Invalid command.") 

    elif userInput == 'r' and hasPlayed == False: 

     print("You have not played a hand yet. Please play a new hand first!") 
     print 

    else: 

     while True: 

      # 
      print 
      playInput = raw_input("Enter u to have yourself play, c to have the computer play: ").lower() 

      if playInput == 'u': 

       print 
       hand = dealHand(HAND_SIZE) 
       playHand(hand, wordList, HAND_SIZE) 
       hasPlayed = True 

      elif playInput == 'c': 

       print 
       hand = dealHand(HAND_SIZE) 
       compPlayHand(hand, wordList, HAND_SIZE) 
       hasPlayed = True 

      else: 

       print("Invalid command.") 
    print 
+1

내가 그것을 호출 생각하지 않는다'* 너무 여러 번 같은 * raw_input', 하지만 그것은 영원히 반복되므로 두 번째로 'nre'중에서 선택할 수있는 기회를 얻지 못할 것입니다. – molbdnilo

답변

1

루프가 완벽하게 작동합니다. 당신이 그것을 말처럼 영원히 반복되는 : 다음 누락 된 무엇

while True: 

, 종료 그 루프 방법입니다. 다른 조건 중 하나를 테스트 : 명시 적으로

playing = True 
while playing: 
    # game 
    # 
    # stop playing with 
    playing = False 

또는이 break 키워드 루프의 탈옥 :

while True: 
    # game 
    # 
    # stop playing with 
    break