'float' object can't be interpreted as int, but converting to int yields no output
First, using range(int(a))
and range(int(a), len(s))
will solve your error. As Jon Clements points out, you can solve that more easily by just using //
instead of /
to get integers in the first place. But either way, it's not causing any problems.
Your problem is that range
s, and just about everything related in Python, are half-open. So, your takeStart
function is returning all the values up to, but not including, the half-way point—that is, it gives you H
for HELLO
, T
for TEST
, BIGG
for BIGGERTEST
.
Just get rid of the -1
on your a = …
lines, and that will solve that problem.
And then it prints out a whole bunch of output lines, all palindromes, which I assume is what you were intending to do.
However, you're still not going to get any odd-length palindromes. For example, with 'MADAM', even when you get the functions right, takeStart(s)
is MA
, takeEnd(s)
is DAM
, flip(takeEnd(s))
is MAD
, and that's not the same as MAD
. Even though your functions are working right, they're not solving the problem. So there's a bug in your design as well as your implementation. If you think about it for a while, you should figure out how to make this work.
And, once you do, you should realize that takeStart
and takeEnd
can be simplified a lot. (Hint: In which cases do you really need to treat odd and even lengths differently?)
While we're at it, this:
foo = ""
for i in range(x, y):
foo += s[i]
return foo
… is just a verbose, slow, and easy-to-get-wrong way of writing this:
return foo[x:y]
And, likewise, your whole flipped
function is just:
return s[::-1]