-2

I have some trouble with the next one (beginnerlevel) slicing an input string in two even pieces or when the length is odd, leave the middle character at the first part.(only boolean)

test=input('giv a a word')

if test[:]%2==0:
    print(str(test)+' is an even word' )
else:
    print(str(test)+' is an**strong text** odd word')

#output: print its an even or odd word and the parts are ... and .....

close=input('press  enter to close')
NutCracker
  • 11,485
  • 4
  • 44
  • 68

1 Answers1

0

In the second line of your code, you wrote if test[:]%2==0:. Using the python slice notation in that form will just give you a copy of the input (and that is why you are getting an exception - you are trying to perform the % operator on an array). You can read more about the python slice notation here.

In order to check for test's length, write:

length = len(test)%2

You can then use the length variable and [:] to slice the input as you want. For example, you wanted to slice the input to two even parts, if it's length is even. So in order to do that you can write: test[:length/2 -1] to get the first half, and test[length/2:length] to get the second half.