Hi i need help for a very easy exercise

python

#1


i need your help for a simple exercice in pyhton 2.7

the exercice is a about strings : you have to check a string under certain condition
the len is bigger or equal 6
4 first characters are equal to the last 4
the 6 th character is R or r
the 3 middle character equal the 3 last characters in inverse sens (abc==cba)
if true --> print Yes
else---> print NO

my code is like that:

str1 = "abff"
#str2="abcdxRdcbM7abcd"

if len(str1) >= 6 and len(str1)%2 != 0 and str1[0:4]==str1[-4:] and str1[5]=="R" or str1[5]=="r" and str1[len(str1)/2-1 : len(str1)/2+2] == str1[:-4:-1]:
    print "Yes"

else:
    print "No"


with str2 everything is ok
but with str1...there is an error:
i have a "index out of range Error"

i cant understand why python is simply not entering into the else line and giving me a "NO" answer
....

Len of str1 is under 6 so he should jump to the else line..


str1 = "abff"
#str2="abcdxRdcbM7abcd"

if len(str1) >= 6 and len(str1)%2 != 0 and str1[0:4]==str1[-4:] and str1[5]=="R" or str1[5]=="r" and str1[len(str1)/2-1 : len(str1)/2+2] == str1[:-4:-1]:
    print "Yes"

else:
    print "No"


#2

if this was your condition:

if len(str1) >= 6 and len(str1)%2 != 0 and str1[0:4]==str1[-4:] and str1[5]=="R":

it would jump to else. but you also have a or keyword:

or str1[5]=="r"

so this condition has to be checked, causing the index problem.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.