In: Computer Science
**To Be Written in Python for Beginners**
Overview Do you know what an alternade is? Of course you don’t, no one does! According to Wikipedia, an alternade: “is a word in which its letters, taken alternatively in a strict sequence, and used in the same order as the original word, make up at least two other words. All letters must be used, but the smaller words are not necessarily of the same length. For example, a word with seven letters where every second letter is used will produce a four-letter word and a three-letter word.” In most cases, the alternade is formed by looking at letters that are two spaces apart, with the first word starting at position 0, and the second from position 1. For example, the word “pained” is an alternade, because using the rules above we produce 2 words: “pie” and “and” Your job is to write a Python program that asks the user to make one of two choices…destruct or construct. If the user chooses destruct, prompt them for an alternade, and then output the 2 words from that alternade. If the user chooses construct, prompt them for 2 words, and then output the alternade that would have produced those words. You must enforce that the users enter real words…or at least not blatant non-words. So if their input contains numbers, whitespace, punctuation, etc., you should prompt them for input until correct input is provided. **You can assume the word entered is of even length, but if you want a challenge, figure out how to implement if the length of the word is even or odd.** If you manage to get all that done, then implement the following: the words output by your program must be in alternating case, starting with uppercase. So, for example, if you wanted to output the word “happy”, it should be displayed as “HaPpY.”
def alternate_word(word):#a function is defined
W2 = ''
for i in range(len(word)):
if i % 2 == 0:
W2 += word[i].upper()#if the word is at even positon then it will be converted to uppercase else it will be converted to lowecase
else:
W2 += word[i].lower()
return W2
choice = input(
'Select your option : \n 1. Enter d for destruct \n 2. Enter c for construct: \n')#enter your choice
if choice.lower() == 'd':
word = input('Enter the alternade: ')
while not word.isalpha():
word = input('Word is not a valid so,try again: ')
W1 = word[::2]
W2 = word[1::2]
print('First word: ', W1)
print('Second word: ', W2)
else:
W1 = input('Enter first word: ')#it will take input of first word from the user
while not W1.isalpha():
W1 = input('Word is not a valid so,try again: ')
W2 = input('Enter second word: ')#it will take input of second word from the user
while not W2.isalpha():
W2 = input('Word is not a valid so,try again: ')
word = ''
for i in range(len(W2)):
word += W1[i]+W2[i]
word += W1[len(W2):]
#alternate_word function is called
print('Alternade is:', alternate_word(word))#it will give the final output
Here is your output screen:
if you have any problem regarding this question then feel free to ask in comment section.if you like the code then give a thumbsup.