Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have two issues with the python program I currently have. On my functions that move items up, down, top or bottom, I need them

I have two issues with the python program I currently have. On my functions that move items up, down, top or bottom, I need them to give a print statement if the item can't be moved to a spot because it is already their i.e. Item can not be moved up anymore because it is at the top. I also need to change how I did these functions to actually swapping items instead of removing and adding them.

''' CSC101 Z2 - Programming Assignment 8 List Maintainer Alan Smith October 22, 2017 Summary This program maintains a list decided upon input from the user. The list does not save. Choose an option from the menu to proceed. ''' def add_item(lst): item = input("Enter item: ") lst.append(item) def list_all(lst): for i in lst: print(i) def remove_item(lst): item = input("Enter item to be removed: ") if item in lst: lst.remove(item) print(item, "is removed.") else: print("Item is not in the list.") def item_up(lst, item): # need to set this function as well as def item_down, def item_top, def item_bottom and def item_swap to actually switch the items locations for i in range(len(lst)): if item in [i]: #not returning the way I need it too print("Item is at the top of the list.") if item in lst: indItem = lst.index(item) moveItem = indItem - 1 lst.insert(moveItem, lst.pop(indItem)) else: print("Item is not in list.") def item_down(lst, item): if item in lst: indItem = lst.index(item) moveItem = indItem + 1 lst.insert(moveItem, lst.pop(indItem)) elif item in range(len(lst) - 1): #not returning for bottom of list print("Item is at the bottom of list.") else: print("Item is not in list.") def item_top(lst, item): if item in lst: indItem = lst.index(item) moveItem = 0 lst.insert(moveItem, lst.pop(indItem)) elif item in lst[0]: #not returning correctly print("Item is at the top of list.") else: print("Item is not in list.")

def item_bottom(lst, item): if item in lst: lst.insert(len(lst), lst.pop(lst.index(item))) elif item in range(0, lst -1): #not returning correctly print("Item is at the bottom of list.") else: print("Item is not in list.") def swap_items(lst): print("Enter two items to swap:") item1 = input("Item 1: ") item2 = input("Item 2: ") if (item1 in lst) and (item2 in lst): temp1 = lst.index(item1) temp2 = lst.index(item2) lst[temp1] = item2 lst[temp2] = item1 else: print("Items not in list.")

def show_menu(): print(''' List Maintainer 3000 Menu: [A]dd item to the list, [R]emove item from list, [U] move an item up, [D] move an item down, [T] move item to the top, [B] move item to the bottom. [L]ist all items, [Q]uit program, [S]wap two items ''') def read_option(): option = input("Enter an option: ").upper() while option not in "ARUDTBLSQ": print("Error: Invalid option. ") option = input("Enter an option: ").upper() return option

def addItem(the_list): item = input("[Enter item: ]") the_list.append(item)

def main(): the_list = [] ##<--list to store user items show_menu() option = read_option() while option != 'Q': if option == 'A': add_item(the_list) print("Item Added.") elif option == 'R': print("Remove An Item.") remove_item(the_list) print("Item Removed.") elif option == 'U': print("Move Item Up") item = input("Enter item to be moved up: ") item_up(the_list, item) print("Item Moved.") elif option == 'D': print("Move Item Down") item = input("Enter item to be moved down: ") item_down(the_list, item) print("Item Moved.") elif option == 'T': print("Move Item To The Top") item = input("Enter item to be moved to the top of list: ") item_top(the_list, item) print("Item Moved.") elif option == 'B': print("Move Item To The Bottom") item = input("Enter item to be moved to the bottom of list: ") item_bottom(the_list, item) print("Item Moved.") elif option == 'L': print("List All") list_all(the_list) elif option == 'S': print("Swap Two Items") swap_items(the_list) print("Items Swapped.") else: print("Bye!") show_menu() option = read_option()

main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Fundamentals Study Guide

Authors: Dr. Sergio Pisano

1st Edition

B09K1WW84J, 979-8985115307

Students also viewed these Databases questions

Question

What is sound?

Answered: 1 week ago