2010-12-29 9 views
0

나는 파이썬 2.6.6을 사용하고있다간단한 동전 던지기 게임을 다시 시작하는 방법

나는 처음부터 사용자 입력을 기반으로 프로그램을 다시 시작하려고하고있다. 감사

import random 
import time 
print "You may press q to quit at any time" 
print "You have an amount chances" 
guess = 5 
while True: 
    chance = random.choice(['heads','tails']) 
    person = raw_input(" heads or tails: ") 
    print "*You have fliped the coin" 
    time.sleep(1) 
    if person == 'q': 
     print " Nooo!" 
    if person == 'q': 
     break 
    if person == chance: 
     print "correct" 
    elif person != chance: 
     print "Incorrect" 
     guess -=1 
    if guess == 0: 
     a = raw_input(" Play again? ") 
     if a == 'n': 
      break 
     if a == 'y': 
      continue 

#Figure out how to restart program 

는 내가 계속 문에 대한 혼란 스러워요. 계속 사용하면 'y'를 처음 입력 한 후에 '계속 재생'옵션이 표시되지 않으므로

+2

다시 시작 하시겠습니까? 전체 애플리케이션을 다시 시작하거나 사용자에게 다른 동전을 던지라고 요청하십시오. – alexn

+0

좋은 지적입니다. – user225312

+0

따라서 전체 응용 프로그램을 다시 시작하십시오. "처음부터" – Tarrant

답변

1

루프를 다시 시작하려는 시점에서 continue 문을 사용하십시오. 루프에서 깨기 위해 break을 사용하는 것처럼 continue 문은 루프를 다시 시작합니다. 귀하의 질문에 따라

하지,하지만 어떻게 continue를 사용 : 또한

while True: 
     choice = raw_input('What do you want? ') 
     if choice == 'restart': 
       continue 
     else: 
       break 

print 'Break!' 

:

choice = 'restart'; 

while choice == 'restart': 
     choice = raw_input('What do you want? ') 

print 'Break!' 

출력 :

당신은을 사용할 필요가
What do you want? restart 
What do you want? break 
Break! 
+0

또한보십시오 : http://stackoverflow.com/questions/1420029/how-to-break-out-of-a-loop-from-inside-a-switch/ 1420100 # 1420100 –

+0

@ 데이브 : 흥미 롭다. 나는 결코 '사실'을 이렇게 보지 않았다. 하지만 파이썬에서는 나쁜 습관입니까? 이 기사가 언어와 무관하다는 사실을 언급하고 있지만 궁금한 점은 궁금합니다. – user225312

+0

@Dave : 어쨌든 나는 여전히 '진정한'을 발견하지만 다른 사람들이 의견을 기다릴 것입니다. – user225312

0

을 사용하여 난수 생성기를 초기화합니다. 매번 같은 값으로 호출하면 random.choice의 값이 반복됩니다.

0

'y'를 입력하면 guess == 0이 True가 될 수 없습니다.

+0

고마워 ... 나는 방금 위에 언급 한 것과 같은 continue 문을 사용하고 그 부분을 작동시키지 만, 나는 여전히 모든 이전의 답변을 고려할 것이다. – Tarrant

1

내가 추천 :

  1. 함수로 코드를 인수 분해; 그것은 도움이 변수 이름
  2. 당신의 정수를 소모하지 않습니다 (당신이 시작하는 방법에 대해 많은 추측을 알고 어떻게 당신의 코드를 통해 처음으로, 후?)

를 사용

  • 가독성이 많이 있습니다.

    import random 
    import time 
    
    GUESSES = 5 
    
    def playGame(): 
        remaining = GUESSES 
        correct = 0 
    
        while remaining>0: 
         hiddenValue = random.choice(('heads','tails')) 
         person = raw_input('Heads or Tails?').lower() 
    
         if person in ('q','quit','e','exit','bye'): 
          print('Quitter!') 
          break 
         elif hiddenValue=='heads' and person in ('h','head','heads'): 
          print('Correct!') 
          correct += 1 
         elif hiddenValue=='tails' and person in ('t','tail','tails'): 
          print('Correct!') 
          correct += 1 
         else: 
          print('Nope, sorry...') 
          remaining -= 1 
    
        print('You got {0} correct (out of {1})\n'.format(correct, correct+GUESSES-remaining)) 
    
    def main(): 
        print("You may press q to quit at any time") 
        print("You have {0} chances".format(GUESSES)) 
    
        while True: 
         playGame() 
         again = raw_input('Play again? (Y/n)').lower() 
         if again in ('n','no','q','quit','e','exit','bye'): 
          break 
    
  • 관련 문제