Python Forum
Code Check? Am I right?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code Check? Am I right?
#1
Hey everyone so this is my homework assignment and I just wanna make sure my code is right because I'm getting some syntax error 

Write a program (PYTHON) that allows the user to add, delete and organize the items in a list.
Your program will provide the following options:

Append: adds an items, input by the user, at the end of the list.
Delete: removes an item indicated by the user from the list.
Move up: moves an item indicated by the user, to the previous position in the list.
Move Down: moves an item indicated by the user to the next position in the list.
Move Top: moves an item indicated by the user to the the first spot in the list.
Move Bottom: moves an item indicated by the user to the last position in the list.
Swap: swaps two items indicated by the user in the list.
List all: display the items in the list; one item per line.
Quit: allows the user to end the program.
Your program shall show appropriate error messages when trying to move, swap, or delete items not found in the list.
You program shall also properly handle invalid actions, like trying to move up the first item in the list, or trying to move down the last item in the list.
In order to get you started, download the file list_maintainer_base.py which you can find at the bottom of this page.
Here is a sample run. Let's assume we already added the following items (in this order): cake, cookies, ice cream.
Adding "donuts"
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, move item to the bottom.
        [L]ist all items,         [Q]uit program,
        [S]wap two items
Enter an option: A

[Add Item]
Enter item: [b]donuts
...Item added.

Listing all items
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, move item to the bottom.
        [L]ist all items,         [Q]uit program,
        [S]wap two items
Enter an option: L

[List All]
[b]01 cake
02 cookies
03 ice cream
04 donuts



here is the code for the list:
def add_item(alist):
print("[Add Item]")
# Read a string from the user
# Add that string to the end of the 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, 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 main():
the_list = [] ##<--list to store user items
  
show_menu()
option = read_option()
while option != 'Q':
if option == 'A':
add_item(the_list)
  
elif option == 'R':
print("...remove item from list")
  
elif option == 'U':
print("...move item up")
  
elif option == 'D':
print("...move item down")
  
elif option == 'T':
print("...move item to the top")
  
elif option == 'B':
print("...move item to the bottom")
  
elif option == 'L':
print("...list all items")
  
elif option == 'S':
print("...swap two items")
  
else:
print("Bye!")
  
show_menu()
option = read_option()

main()
[/b][/b]


This is my code:

def add_item(alist):

    print("[Add Item]")
    item = input("Enter Item:")
    for i in alist:
        alist.insert(i,item) #Adding Item to the List.syntax
        i = i + 1 # This keeps incrmenting the index value
    return alist
  
def del_item(alist):
    element = input("Enter the item you want to delete:")
    try:
        if element in alist:
            number = alist.index(element)
            del alist[number]
    except:
        print("That Item is not in list")
    return alist
  
def move_up(alist):
    element = input("Enter the item to want to move up:")
    element_position = alist.index(element)
    if(element_position <= len(alist)):
        alist.insert(element_position-1,element)
        del alist[element_position]
    elif (element_position == 0)
        print("That item can't be moved as it is in top of the list..!!")
    return alist

def move_down(alist):
    element = input("Enter the item to want to move down:")
    element_position = alist.index(element)
    if(element_position <= len(alist)):
        alist.insert(element_position+1,element)
        del alist[element_position]
    elif (element_position == max(len(alist)))
        print("That item can't be moved further down, it is in already in bottom of the list..!!")
    return alist

def move_top(alist):
    element = input("Enter the item to want to move down:")
    element_position = alist.index(element)
    if(element_position == 0):
        alist.insert(0, element)
        del alist[element_position]
    else:
        print("The item is already on top position in the list")
    return alist

def move_bottom(alist):
    element = input("Enter the item to want to move down:")
    element_position = alist.index(element)
    if(element_position < len(alist)):
        alist.insert(max(len(alist))),element)
        del alist[element_position]
    elif (element_position == max(len(alist)))
        print("It is in already in bottom of the list..!!")
    return alist

def swap(alist):
    element_1 = input("Enter the first item:")
    element_2 = input("Enter the second item to swap:)
    a, b = i.index(element_1), i.index(element_2)
    try:
        for i in alist:
            i, i[a] = i[a], i[b]
        print alist
      
    except:
        print("You entered worng elements")
  
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 main():
    the_list = [] ##<--list to store user items
  
    show_menu()
    option = read_option()
    while option != 'Q':
        if option == 'A':
            add_item(the_list)
          
        elif option == 'R':
          
            print("...remove item from list")
            del_item(the_list)
          
        elif option == 'U':
          
            print("...move item up")
            move_down(the_list)
          
        elif option == 'D':
          
            print("...move item down")
            move_down(the_list)
          
        elif option == 'T':
          
            print("...move item to the top")
            move_top(the_list)
          
        elif option == 'B':
            move_bottom(the_list)
            print("...move item to the bottom")
          
        elif option == 'L':
            print("...list all items")
            for elem in the_list:
                 print(elem)
          
        elif option == 'S':
            print("...swap two items")
            swap(alist)
          
        else:
            print("Bye!")
            exit()
          
        show_menu()
        option = read_option()

[b]main()       
[/b][/b][/b]
Reply
#2
Please put code in code tags and re-post
Reply
#3
As Larz60+ points out, please post between the code tags as well as the complete error message you are getting between the error tags. Instructions can be found in the Help documents or by pressing the "BBCode Help" button at the bottom of the edit window.  Also, there seems to be "formatting" in your code, to prevent this, when pasting your code between the tags, use " CTRL + SHIFT + V ".

Finally, it's not necessary to post the entire lesson, just the relevant code Smile .
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
(Mar-09-2017, 02:47 PM)Larz60+ Wrote: Please put code in code tags and re-post
And remove the formatting so that your post uses the standard forum typography.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 2,621 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  Please check code Evgeniy2019 4 3,090 Apr-05-2019, 06:37 PM
Last Post: Evgeniy2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020