how to return last condition if other 2 not met?
On 2018-05-24 00:57, asa32sd23 at gmail.com wrote:
> i want to check/return for 3 conditions, it loops shortest str and finds diff in other
> 1. if difference is immediate before end of range, return index, exit
> 2. if string length is same and index loop is done, return 'identical'
> 3. if neither of above is found. it means the short loop ended and every letter was same so next letter of longer str is the diff, just return idex+1
> but my last print statement always print. not sure how to end this
> str1= "kitti cat"
> str2= 'kitti catt'
> lenStr1= len(str1)
> lenStr2= len(str2)
> #find shortest str and loop range with this one
> if lenStr1 >= lenStr2:
> str= str2
> str= str1
> # loop each character of shortest string, compare to same index of longer string
> # if any difference, exit and return index of difference
> for idx in range(len(str)):
> a= str1[idx]
> b= str2[idx]
> if a != b: #immeditely exit, since non-match found
> if len(str1) == len(str2) and idx == len(str1)-1: #if no difference print 'identical'
The 'for' loop (and also the 'while' loop) can have an 'else' clause,
which is run if it didn't break out of the loop:
for idx in range(len(str)):
# body of loop
# didn't break out of the loop