Reputation: 11
Ask the user for the input of a word with a minimum of 3 characters. Take that word and exchange the MIDDLE letter with the LAST letter of the word. If the word is an even number of characters long, take the letter to the right of middle (IE if a word is 6 letters long, you want the 4th character: “switch” is 6 characters and we’d want the ‘t’ as our middle character. Output to the user their newly rearranged word. *additional difficulty perform a check that the word is at LEAST 3 letters and display a message that it’s not long enough if something shorter than 3 characters is displayed.strong text
can you guys help me with this?
My code so far:
word=input('Please enter a word with a minimum of 3 characters')
word_length=len(word)
word_middle_index = int(word_length//2)
print('The letter in the middle of the word "'+word+'" is:
word[word_middle_index])
this is how much I've done
Upvotes: 1
Views: 115
Reputation: 1731
For making this easier to grasp, I haven't used any functions, classes, encapsulations or DRY principle here. Since you wanted to show helpful messages in each case, I have also added them.
while True:
word = input("Put a word with at least 3 letters: ")
# check if the word has at least 3 letters
if len(word) < 3:
print("Word needs to be at least 3 letters long!!")
break
# check odd
if not len(word) % 2 == 0:
print("Word length is odd!!!")
# make a list of letters
word_lst = [letter for letter in word]
# mid index
mid_ix = (len(word) - 1) // 2
# last index
last_ix = len(word) - 1
# swap middle and last character
word_lst[mid_ix], word_lst[last_ix] = word_lst[last_ix], word_lst[mid_ix]
# make the list into a string again
word = "".join(word_lst)
print(word)
break
# check even
else:
print("Word length is even!!!")
# make a list of letters
word_lst = [letter for letter in word]
# mid index
mid_ix = (len(word) - 1) // 2 + 1
# last index
last_ix = len(word) - 1
# swap middle and last character
word_lst[mid_ix], word_lst[last_ix] = word_lst[last_ix], word_lst[mid_ix]
# make the list into a string again
word = "".join(word_lst)
print(word)
break
Upvotes: 0
Reputation: 920
I've done it so that you can learn how to approach such a problem :
I'd recommend looking into a couple things online :
len
function
%
operator
[:] operator
For instance :
n % 2 == 0
is True when is even.
"abcd"[1:4] returns "bcd"
word = input("Enter A Word: ")
if len(word) < 3:
print("The word length as to be more than 3 characters")
else:
newWord = ""
middleLetterIndex = 0
lastLetterIndex = len(word) - 1
if len(word) % 2 == 0: # even
middleLetterIndex = int(len(word) / 2) + 1
else:
middleLetterIndex = int(len(word) / 2)
middleLetter = word[middleLetterIndex]
lastLetter = word[lastLetterIndex]
newWord = word[:middleLetterIndex]
newWord += lastLetter
newWord += word[middleLetterIndex+1:lastLetterIndex]
newWord += middleLetter
print(newWord)
Hope this helps you!
Upvotes: 1
Reputation: 1032
this function will complete the task for you:
def switch_mid_and_last(word):
if len(word) < 3:
return("too short word")
else:
mid_letter = len(word)//2
new_word = word[:mid_letter] + word[-1] + word[mid_letter+1:-1] + word[mid_letter]
return(new_word)
the outputs for the next inputs are:
print(switch_mid_and_last("ab"))
>>> too short word
print(switch_mid_and_last("abc"))
>>> acb
print(switch_mid_and_last("abcd"))
>>> abdc
print(switch_mid_and_last("abcde"))
>>> abedc
print(switch_mid_and_last("abcdef"))
>>> abcfed
Upvotes: 1