Question: 4 questions in python: 1. Write a program that keeps asking the user for new values to be added to a list until the user

4 questions in python:

1. 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'.

2.

Write a function called 'return_list' that takes a string as a parameter and returns a list of words in the string. "Words" are entities that are seperated by either commas (',') or spaces (' '). In case the string contains neither commas nor spaces, the string will be returned without any manipulation. Note: We are not testing for the case where both commas and spaces are present in the string so feel free to ignore that.

Example:

Input:

Enter the string: this is a string

Output:

['this', 'is', 'a', 'string']

Input:

Enter the string: Pranshu,Enbody,Alireza

Output:

['Pranshu', 'Enbody', 'Alireza']

3.

The main() module in the starter code below takes integar inputs separated by commas from the user and stores them in a list. Then, it allows the user to manipulate the list using 3 functions:

mutate_list() takes 3 parameters -- a list, index number, and a value -- and inserts the value in the position specified by the index number in the list.

remove_index() takes 2 parameters -- a list and an index number -- and remove the element at the position number indicated by index. It also prints the total number of elements in the list before and after removing the character in this fashion:"Total elements in list = 11 Total elements in list = 10"

reverse_list() takes 1 parameter -- a list -- and returns the list reversed.

Examples:

Example 1:

Enter values in list separated by commas: 1,2,4,63,6,4,22,53,76 [1, 2, 4, 63, 6, 4, 22, 53, 76] Menu: mutate list(m), remove (r), reverse_list (R)

Enter choice (m,r,R): m

4,45 [1, 2, 4, 63, 45, 6, 4, 22, 53, 76]

Example 2:

Enter values in list separated by commas: 1,2,4,6,84,3,2,2,76 [1, 2, 4, 6, 84, 3, 2, 2, 76] Menu: mutate list(m), remove (r), reverse_list (R)

Enter choice (m,r,R): R [76, 2, 2, 3, 84, 6, 4, 2, 1]

Example 3:

Enter values in list separated by commas: 12,2,3,5,2,6,2,1,2,333,65 [12, 2, 3, 5, 2, 6, 2, 1, 2, 333, 65] Menu: mutate list(m), remove (r), reverse_list (R)

Enter choice (m,r,R): r

4 Total elements in list = 11 Total elements in list = 10 [12, 2, 3, 5, 6, 2, 1, 2, 333, 65]

4.

Write a function that takes a list as a parameter, converts every element in the list to integar and then returns a tuple comprising of these integar elements. If the function encounters a character such as 'p' that cannot be converted to an integar, it throws this error message on the screen: "Error. Please enter only integers."

Example:

Input:

Enter elements of list separated by commas: 1,2,3,5

Output: (1, 2, 3, 5)

Input:

1,2,d,5

Output:

Error. Please enter only integers.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!