Question

In: Computer Science

use python to solve Write a function called word_chain that repeatedly asks the user for a...

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!

Solutions

Expert Solution

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.


Related Solutions

(IN PYTHON) Write a program that asks the user repeatedly to enter a student's score or...
(IN PYTHON) Write a program that asks the user repeatedly to enter a student's score or enter -1 to stop. When finished entering all the scores, the program should display the number of scores entered, the sum of the scores, the mean, the lowest and the highest score. (IN PYTHON)
Please write in python Use modular design to write a program that asks the user to...
Please write in python Use modular design to write a program that asks the user to enter his or her weight and the name of a planet. The program then outputs how much the user would weigh on that planet. The following table gives the factor by which the weight must be multiplied for each planet. PLANET CONVERSION FACTOR Mercury 0.4155 Venus 0.8975 Earth 1.0000 Moon 0.1660 Mars 0.3507 Jupiter 2.5374 Saturn 1.0677 Uranus 0.8947 Neptune 1.1794 Pluto 0.0899 The...
Write C++ void function called averageTemperature() that asks the user to enter integers with a sentinel...
Write C++ void function called averageTemperature() that asks the user to enter integers with a sentinel value of -999 to stop entering numbers. Then average the numbers and print the average. You will need an int acculumator for the temperatures and an int acculumator for the number of temperatures entered. Use the accumulators to calculate the average which will need to be a decimal number. Call the function from the main() function.
in python, sorry i thought i wrote it Write an interactive program that repeatedly asks the...
in python, sorry i thought i wrote it Write an interactive program that repeatedly asks the user to input a number until”Enter” is hit. Your program should create a file named “numbers.txt” where all the numbersare written one below the other, and the last line should display the sum of all the input, afterthe string “The sum of your numbers is ”.For example, if the user inputs 3, 5, 7, 10, -3, 5.2, then the file “numbers.txt” should contain: 3...
In Python write a program that asks the user to enter the monthly costs for the...
In Python write a program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payment, insurance, gas, oil, tires, and maintenance the program should then display the total monthly cost of these expenses, and the total annual cost of these expenses. your program MUST have BOTH a main function AND a function named calcExpenses to calculate the expenses. DO NOT display the expenses inside of the calcExpenses function!!...
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
In a file called LengthSum.java, write a program that: - Asks the user to enter a...
In a file called LengthSum.java, write a program that: - Asks the user to enter a string. - Asks the user to enter a second string. - Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please...
In Java:Implement a program that repeatedly asks the user to input apositive integer and...
In Java:Implement a program that repeatedly asks the user to input a positive integer and outputs the factorial of that input integer. Do not use recursion, solution should use stack.
Write a design algorithm and a python program which asks the user for the length of...
Write a design algorithm and a python program which asks the user for the length of the three sides of two triangles. It should compute the perimeter of the two triangles and display it. It should also display which triangle has the greater perimeter. If both have the same perimeter it should display that the perimeters are the same. Formula: Perimeter of triangleA = (side1A + side2A +side3A) See the 2 sample outputs. Enter side1 of TriangleA: 2 Enter side2...
Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT