2014-04-27 6 views
-4

Udacity CS101 과정 (Python)을 수강하고 있습니다. 아래의 문제와 해결책은 "2 단원 : 문제 세트 - 마지막 찾기"while 루프는 언제 멈출 지 어떻게 알 수 있습니까?

내 질문 : 아래 코드의 while 루프는 언제 멈출 지 어떻게 알 수 있습니까?

# Define a procedure, find_last, that takes as input 
# two strings, a search string and a target string, 
# and returns the last position in the search string 
# where the target string appears, or -1 if there 
# are no occurences. 
# 
# Example: find_last('aaaa', 'a') returns 3 

# Make sure your procedure has a return statement. 



def find_last(s,t): 
    last_pos = -1 
    while True: 
      pos = s.find(t, last_pos+1) 
      if pos == -1: 
       return last_pos 
      last_pos = pos 
+0

요즘 누가 '프로 시저'라는 단어를 사용합니까? – devnull

+0

70 년대 이래로 학교 밖에서 프로그래밍되지 않은 @devnull 교수님들? : p – keyser

+0

* procedure *의 엄격한 정의가 있지만 함수가 값을 반환하기 때문에 여기서 수행되지 않습니다. – tripleee

답변

1

while 루프 함수가 종료되면 중지하고 return 명령문이 실행되면 기능이 종료한다.

return 명령문이 실행될 때 s.find() 복귀 -1 이후 last_pos + 1에서 검색 할 때 ts에서 발견되지 않았 음을 의미하지 않는다.

1

결코 멈추지 않습니다. return 문을 실행하면 강제로 종료됩니다.

0

함수에서 return을 호출하면 함수의 실행이 종료됩니다.

관련 문제