In: Computer Science
use python to solve
Write a function called word_chain that repeatedly asks the user for a word until either (1) the user has entered ten words, or (2) the user types nothing and presses enter. Each time the user enters an actual word, your program should print all the words entered so far, in one long chain. For example, if the user just typed "orange", and the user has already entered "apple" and "banana", your program should print "applebananaorange" before asking for the next word. Each of your prompts should look like this:
Enter word:
Your function MUST be called word_chain. You can write code in the main part of your program to test your function, but when you submit, your code must ONLY have the function definition!
ANSWER:
Function definition:
def word_chain():
l=[]
while(len(l)!=10):
s=input("Enter word:")
if s=="":
break
else:
l.append(s)
print("".join(l))
NOTE: The above function is written in Python3. Please refer to the attached screenshots for code indentation.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Code with main function:
def word_chain():
l=[]
while(len(l)!=10):
s=input("Enter word:")
if s=="":
break
else:
l.append(s)
print("".join(l))
if __name__=="__main__":
word_chain()
NOTE: The above code is in Python3. Please refer to the attached screenshots for code indentation and sample I/O.
SAMPLE I/O:
1. User entered 10 strings:
2. User entered 2 words and then the user pressed enter.