Hi i need help for a very easy exercise

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

i need your help for a simple exercice in pyhton 2.7

<Below this line, add a link to the EXACT exercise that you are stuck at.>

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"

<In what way does your code behave incorrectly? Include ALL error messages.>
with str2 everything is ok
but with str1…there is an error:
i have a “index out of range Error”

<What do you expect to happen instead?>

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…

```python

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”

<do not remove the three backticks above>

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.

1 Like

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