2017-03-23 1 views
1

저는 컴퓨터 과학에 대한 소개 과정을 밟고 있으며 지금은 파이썬을보고 있습니다. 우리는 단지 연습을 위해 간단한 게임을 만들어야했습니다. 내 문제는 플레이어가 $ 0에 도달하면 while 루프를 끝내고 프로그램을 종료하는 것입니다. 내 프로그램은 $ 0을 지나서 $ 5를 계속 감축합니다. 나는 도움이 필요하다!while 루프가 특정 조건을 충족하면 프로그램을 어떻게 종료합니까?

print ("Welcome to this game!") 
password = input ("please put in $50 for 10 tries to play the game,"\ 
         "then type ok.") 
while password != ("ok"): 
    password = input ("Sorry, wrong password. Please put in $50 for 10 tries to play the game,"\ 
         "then type ok.") 
    if password== ("ok"): 
     print ("Time to play") 

import random 
answer=random.randrange(50)+1 

total=50 

tries=1 
guess=int(input("Guess the number and win $100: ")) 
while guess !=answer: 
    print ("you loose $5") 
    if guess < answer: 
     print ("too low, you now have", total-5, "dollars left.") 
    elif guess > answer: 
     print ("too high, you now have", total-5, "dollars left.") 
    guess=int(input("Guess the number and win $100: ")) 
    tries=tries+1 
    total=total-5 

print ("well done, you have won $100 in", tries,"tries") 
+1

'total == 0 : break;'또는 더 좋은 경우'total == 0 : break;' – umutto

+1

도 추측! = 답변 및 합계 <= 0 : ' –

+2

break 키워드를 사용하십시오. –

답변

4

break 키워드는 가장 작은 둘러싸는 루프를 종료합니다.

관련 문제