Welcome to Westonci.ca, the place where your questions find answers from a community of knowledgeable experts. Join our platform to connect with experts ready to provide precise answers to your questions in different areas. Connect with a community of professionals ready to provide precise solutions to your questions quickly and accurately.

Write a program that keeps asking the user for new values to be added to a list until the user enters 'exit' ('exit' should NOT be added to the list). These values entered by the user are added to a list we call 'initial_list'. Then write a function that takes this initial_list as input and returns another list with 3 copies of every value in the initial_list. Finally, inside main(), print out all of the values in the new list.

Sagot :

Answer:

def create_3_copies(initial_list):

   another_list = []

   

   for i in range(3):

       for x in initial_list:

           another_list.append(x)

   return another_list

   

initial_list = []

while True:

   value = input("Enter a value: ")

   if value == "exit":

       break

       

   initial_list.append(value)

for x in create_3_copies(initial_list):

    print(x, end=" ")

Explanation:

Create a function that takes initial_list as parameter. Inside the function:

Create an empty list called another_list

Create a nested for loop. The outer loop iterates 3 times (Since we need to add every value 3 times). The inner loop iterates through the another_list and adds each value to the another_list.

When the loops are done, return the another_list

In the main:

Create an empty list named initial_list

Create an indefinite while loop. Inside the loop, ask the user to enter a value. If the value equals "exit", stop the loop. Otherwise, add the value to the initial_list

Create another for loop that calls the function we created passing the initial list as a parameter and iterates through the values that it returns. Inside the loop, print each value