Another use of return

#Example: The loan is repaid every month for 10 years, and it is fully repaid in 5 years and 5 months. 
#When year=5,yue=5, the entire cycle is terminated. The implementation method is as follows: 
#Method 1 
jieshu=0 
for year in range(1,11): 
    for yue in range(1,13): 
        if year==5 and yue==5: 
            jieshu=1 
            break# can only terminate the inner loop, not the outer loop 
        print("th" ,year,"year,th",yue,"month") 
    if jieshu==1: 
       break 
#Method 2 
def fun5(): 
    for year in range(1, 11): 
        for yue in range(1, 13): 
            if year == 5 and yue == 5: 
                return # It can terminate the inner loop as well as the outer loop, because the function will no longer be executed after return. 
            print("th", year, "year, th" , yue, "month") 
fun5()

Guess you like

Origin blog.csdn.net/qq_40333984/article/details/125432947