161k views
3 votes
g 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. For example: Input: Enter value to be added to list: a Enter value to be added to list: b Enter value to be added to list: c Enter value to be added to list: exit Output: a b c a b c a b c Note how 'exit' is NOT added to the list. Also, your program needs to be able to handle any variation of 'exit' such as 'Exit', 'EXIT' etc. and treat them all as 'exit'.

User Timothym
by
3.8k points

1 Answer

3 votes

Answer:

Following are the code to the given question:

def list_function(initial_list):#defining a method list_function that takes list in parameter

l = initial_list*3#defining l that multiple the list by 3

return l#return list

def main(): #defining main method

initial_list = []#defining an empty list

while True:#defining while loop

i = input("Enter value to be added to list: ")#defining i variable that input list value

i = i.rstrip()#use rstrip method

if i.lower() == "exit" and "EXIT" and "Exit":#defining if block that checks list value is in lower case

break#use break keyword

initial_list.append(i)#use list that adds input value

l = list_function(initial_list)#defining l variable that calls the list_function method

for i in l:#defining for loop that print list items

print(i)#print value

main()

Output:

Please find the attached file.

Step-by-step explanation:

In this code, a method "list_function" is declared that takes list "initial_list" in parameter and defines "l" variable that multiple the list by 3 and return its list value.

In the main method an empty list "initial_list" is declared that defines a while loop that input list value and use rstrip method and define and if block that checks list value is in lower case and pass into the method and use for loop to prints its values.

g Write a program that keeps asking the user for new values to be added to a list-example-1
User Atul Chaudhary
by
3.4k points