2014-03-05 4 views
-3
sides=int(input("Please enter the amount of sides on the dice: ")) 
times=int(input("Please enter the number of times you want to repeat: ")) 

사용자가 각 요청에 대해 int 번호를 입력 할 때까지 두 줄을 반복하고 싶습니다.
감사 느슨하게 읽기파이썬에서 반복하는 방법

+0

좋은 장소까지 시작은 [while loops] (https://wiki.python.org/moin/WhileLoop)에 대해 배우는 것입니다. 그런 다음 http://stackoverflow.com/questions/1265665/python-check-if-a-string-represents-an-int-without-using-try-except –

+1

에서 자습서 및 [ documentation] (http://docs.python.org/2/tutorial/controlflow.html#for-statements)을 참조하십시오. 언어의 기본 토대를 배울 때까지 Stack Overflow를 최대한 활용하지 못할 것입니다. – mhlester

+0

do-while 루프와 비슷한 것 – user3358812

답변

2

,

def get_int(prompt): 
    while True: 
     try: 
      return int(input(prompt)) 
     except ValueError: # wasn't an int 
      pass 

sides = get_int("Please enter the amount of sides on the dice: ") 
times = get_int("Please enter the number of times you want to repeat: ") 

하지만 당신은 엄격하게 모두 문을 반복합니다 모두 얻을 int 값,

while True: 
    s1 = input("Please enter the amount of sides on the dice: ") 
    s2 = input("Please enter the number of times you want to repeat: ") 
    try: 
     sides = int(s1) 
     times = int(s2) 
     break 
    except ValueError: 
     pass 
0
sides=0;times=0 
while(sides == 0 and times == 0): 
    sides=int(input("Please enter the amount of sides on the dice: ")) 
    times=int(input("Please enter the number of times you want to repeat: ")) 
+0

오류를 반환하고 코드를 다시로드해야합니다. 사용자가 각 요청에 대해 int 번호를 입력 할 때까지 두 줄을 반복하고 싶습니다. – user3358812

+1

사용자가 코드를 int()로 지정했기 때문에 오류가 발생합니다. 번호가 아닌 경우 어떻게됩니까? 파이썬은 오류를 던질 것입니다. ['try/catch'] (http://docs.python.org/3.2/tutorial/errors.html) 진술을 읽고 그 오류를 잡아 실행을 계속하십시오. –

관련 문제