2014-04-01 5 views
1

나는이 프로젝트에이 코드를 제작하고 그냥 처음 입력 기능을 반복 변경할 수 없기 내가 while 루프 문제로 실행 한 반면, 여기에 코드가, 나는 경우를 aprriecate 것이다 사람 내 문제를 지적하고 내 코드를 해결하는 데 도움 수 roll_agn 루프 내부에 '비 'yes이 된 경우에만 동안의 다른 블록이 실행됩니다무한 주사위 롤 프로그램에 대한 루프 문제가

import random 
roll_agn='yes' 
while roll_agn=='yes': 
    dice=input ('Please choose a 4, 6 or 12 sided dice: ') 
    if dice ==4: 
     print(random.randint(1,4)) 
    elif dice ==6: 
     print(random.randint(1,6)) 
    elif dice ==12: 
     print(random.randint(1,12)) 
    else: 
     roll_agn=input('that is not 4, 6 or 12, would you like to choose again, please answer yes or no') 
    if roll_agn !='yes': 
     print ('ok thanks for playing') 
+0

들여 당신의'다른'while 루프 내부에하고이 문제가 해결됩니다. –

답변

1

을 thnx. 당신은 while 루프 내부를 변경하지 않을, 그래서 영원히 반복됩니다.

0

else 문은 루프 밖에서 들여 쓰기되지 않으므로 변수가 재설정되지 않으므로 while 루프에 필요한 조건은 항상 True이므로 무한 루프가됩니다. 들여 쓰기가 필요합니다.

elif dice ==12: 
    ... 
else: 
^ roll_agn = input() 
0

들여 쓰기가 꺼져 있습니다. 여기

import random 


while True: 
    try: 
     dice = int(input ('Please choose a 4, 6 or 12 sided dice: ')) # this input should be an int 
     if dice in (4, 6, 12): # checks to see if the value of dice is in the supplied tuple 
      print(random.randint(1,dice)) 
      choice = input('Roll again? Enter yes or no: ') 
      if choice.lower() == 'no': # use .lower() here so a match is found if the player enters No or NO 
       print('Thanks for playing!') 
       break # exits the loop 
     else: 
      print('that is not 4, 6 or 12') 
    except ValueError: # catches an exception if the player enters a letter instead of a number or enters nothing 
     print('Please enter a number') 

이 상관없이 플레이어가 들어가 무엇을 작동하지 않습니다 조금 더 코드를 개선하는 방법에 대한 몇 가지 제안입니다.