Reputation: 1
Good day fellow programmers!
This is the instructions I got for my python class and I am struggling so hard to make the input appear as I'd like.
flipside(s) takes a string s and returns a string whose first half is s's second half and the second half is the first. if the length is odd, the first half of the input string should have one fewer character than the second half. For example, the string carpet would have the output petcar. The input carpets would have the output petscar.
This is my current code:
def flipside():
print("This program will return second half as first half of what you write.")
s = input("Type in any word: ")
newWord = len(s)%2
print("DEBUG: ",newWord)
if newWord == 1:
print("DEBUG: ODD")
print (s[3:]+s[:4])
else:
print("DEBUG: EVEN")
print(s[4:]+s[:4])
I am really confused of what I should type inside the brackets to slice up the words properly. I've searched around using google to find solution for this function, and none work.
Could any experienced Python programmer explain to me what I've done wrong with my code? I'd like to learn.
I am using python 3.0
Upvotes: 0
Views: 300
Reputation: 77865
This is not a matter of Python programming, just of finding the division point. Whatever you find, your new string will be of the second form you give:
s[div_pt:] + s[:div_pt]
The "ODD" case you give repeats the 4th letter.
Integer division should solve your problem for you:
div_pt = len(s) // 2
Can you slip those into your program and see what you get? Try a couple of different examples, such as "carpet" and "ashtray".
Upvotes: 1