Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a function called loops that accepts four arguments: (a) an integer named start, (b) an integer named stop, (c) an integer named step,
Write a function called loops that accepts four arguments: (a) an integer named start, (b) an integer named stop, (c) an integer named step, and (d) an iterable object (a list of ordered elements) named iterable. This function returns None. The function must inspect the step value and swap start and stop values so that it always produces a non-empty sequence of numbers, i.e. if the value of step is positive, the value of start must be less than the value of stop. If start is greater than stop - switch the values. It must first use a for loop to display the sequence of numbers horizontally and a while loop to display the sequence of numbers horizontally that includes the start and stop values. 1>>> [evaluate Lab1_JC.py] 2>>> iterable = [0, 1, 2, 3, 4] 3>>> loops (4, 2, 1, iterable) Output for 'for' loop: 234 Output for 'while' loop: 234 3) [20 marks] Write a function named main that takes no parameters and returns None. This function displays a menu (see sample output for menu), gets a choice from the user, and executes that choice. Choice 1 will demonstrate the functions for loops. The user should be asked for a choice until '5' is entered, which displays 'Goodbye' and terminates. Within this function, create the following iterable object: iterable = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 1) [35 marks] Write a function called get_input that accepts an iterable object (a list of ordered elements) named iterable as its parameter and returns a 3-tuple. This function prompts the user for the following three values: (a) the letter 'A' or 'D', which will indicate ascending or descending (respectively), (b) an integer between 0 and the number of items in iterable, and (c) another integer between 0 and the number of items in iterable object. The function returns a 3-tuple: the first integer (as an integer), the second integer (as an integer), and either -1 (if the user entered 'D') or 1 (if the user entered 'A'). The function must perform error checking for all inputs and must accept both lower and uppercase characters for the first question. 1>>> [evaluate Lab1_JC.py] 2>>> iterable = [0, 1, 2, 3, 4] 3>>> val_1, val_2, step = get_input(iterable) Enter 'A' (for ascending) or 'D' (for descending): p Enter 'A' (for ascending) or 'D' (for descending): @ Enter 'A' (for ascending) or 'D' (for descending): 2.0 Enter 'A' (for ascending) or 'D' (for descending): Enter 'A' (for ascending) or 'D' (for descending): a Enter an index: -1 Invalid index. Enter one between 0 and 4: 7.3 Invalid index. Enter one between 0 and 4: 17 Invalid index. Enter one between 0 and 4: 4 Enter another index: 3 4>>>print(val_1, val_2, step) 4 31
Step by Step Solution
★★★★★
3.42 Rating (149 Votes )
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started