Python Forum
i need help at the part where i have to use a function and initialise the menu.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i need help at the part where i have to use a function and initialise the menu.
#1
i am doing an assignment for a ESP system , something like a grocery cart where i can list the items, add to cart, display cart etc.
can anyone help me with the initialising part where i have to use a function and initialise the menu dictionary with items and price values.
Larz60+ write May-31-2022, 01:10 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

.py   ESPsystem.py (Size: 3.83 KB / Downloads: 118)
Reply
#2
Include your code in the post, do not link to files.
Reply
#3
def stop():
    print('\n\n\nThank you for using ESP and goodbye! ')
#   exit(0)

def init(): #initialise the menu dictionary with item: price values...
    menu = {'Firewall Services':1.2,'Security Ops Center':4.2,'Hot Site':8.5,'Data Protection':10.0}
    

def main(s='no services'): #display menu and prompt user for choice
    choice='0'
    while choice!='' and choice!='5':
        print(f"\n\n===========================================\n"+
            "Welcome to Electronics Services & Protection:\n"+
            "===============================================\n")
        choice = input('''
    1. Display Our list of Services
    2. Search for service
    3. Display added services
    4. Payment
    5. Exit ESP
    6. Display Services currently subscribed



    Please input your choice of action(ENTER to exit):''')
        if choice == '1':
            #displayMenu()
            addOrder(list(menu.keys()))
        elif choice == '2':
            search()
        elif choice == '3':
            displayCart()
        elif choice == '4':
            checkout()
        elif choice == '6':
            input(f"You have currently subscribed to: \n{s}\nPress Enter key to continue... ")
        elif (choice ==''or choice =='5'):
            stop()

def checkout(): #list the cart and total the amount to pay
    if (len(order)>0):
        print("please check your services added:\n")
        total=0
        for i,item in enumerate(order):
            print(f"{i+1}. {item:20}:\t${menu[item]}k/year")
            total+=menu[item]
        order.clear()
        input(f"Your subscripton will be a total of : ${total}k/year. \nThank you for using ESP.")
    else:
        print('You have not added any service!')

def displayCart(): #display menu for the day
    if len(order) == 0:
        input('You have not added any service! Press enter to continue...')
        return
    print(f"Services you have added today: ")
    for i, item in enumerate(order,1):
        print(f"{i}. {item:20}:\t${menu[item]}k")
    choice = 1
    while (choice)!=0:
        choice=eval(input(f"Enter the service 1-{i} that you would like to remove, or 0 to stop: "))
        if choice>=0 and choice<=i:
            if choice>0:
                print(f"{order.pop(choice-1)} removed")
                return
            print(order)
        else:
            print(f"Invalid choice! Please enter 1-{i} or 0 to stop")

    print()
    input("Press any key to continue...")

def search():
    choice= " "

    while len(choice)>0:
        choice = input("\n\nPlease input service to search: ")
        if len(choice)==0:
            break

    matches=[]

    for service in menu.keys():
        if service.lower().find(choice.lower())>=0:
            matches.append(service.strip())

    if len(matches)==0:
        print(f"Sorry we don't have services: {choice}!\n")
        choice=input("Please press ENTER key to quit or 'N' enter new search: ")
    else:
        addOrder(matches)
        

def addOrder(matches):
    submenu=""
    for i,service in enumerate(matches,1):
        submenu+=f"\n{i}. {service:20}:\t${menu[service]}k/year"

    print(f"yes, we have the following services(s):{submenu}")
    choice=-1
    while (choice)!=0:
        choice=0 #indicate exit case
        choice=eval(input(f"Enter the service 1-{i} that you would like to add, or 0 to stop: "))
        if choice>=0 and choice<=i:
            if choice>0:
                order.append(matches[choice-1])
                print(f"{choice} added")
            #print(order)
        else:
            print(f"Invalid choice! Please enter 1-{i} or 0 to stop")
            
    print(f"You have added: {order}")

order=[] #cart
menu={} #menu
if __name__ == '__main__':
    init() #populate menu
    main() #display welcome screen
Larz60+ write Jun-02-2022, 10:46 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
2nd notice, Please do not ignore. This is a forum rule.
I have done this for you this time.
Reply
#4
Closer, try again. Put it between the Python tags so it preserves indentation (the blue and yellow icon above the text box in Reply)
Reply
#5
def stop():
print('\n\n\nThank you for using ESP and goodbye! ')
# exit(0)

def init(): #initialise the menu dictionary with item: price values...
menu = {'Firewall Services':1.2,'Security Ops Center':4.2,'Hot Site':8.5,'Data Protection':10.0}


def main(s='no services'): #display menu and prompt user for choice
choice='0'
while choice!='' and choice!='5':
print(f"\n\n===========================================\n"+
"Welcome to Electronics Services & Protection:\n"+
"===============================================\n")
choice = input('''
1. Display Our list of Services
2. Search for service
3. Display added services
4. Payment
5. Exit ESP
6. Display Services currently subscribed



Please input your choice of action(ENTER to exit):''')
if choice == '1':
#displayMenu()
addOrder(list(menu.keys()))
elif choice == '2':
search()
elif choice == '3':
displayCart()
elif choice == '4':
checkout()
elif choice == '6':
input(f"You have currently subscribed to: \n{s}\nPress Enter key to continue... ")
elif (choice ==''or choice =='5'):
stop()

def checkout(): #list the cart and total the amount to pay
if (len(order)>0):
print("please check your services added:\n")
total=0
for i,item in enumerate(order):
print(f"{i+1}. {item:20}:\t${menu[item]}k/year")
total+=menu[item]
order.clear()
input(f"Your subscripton will be a total of : ${total}k/year. \nThank you for using ESP.")
else:
print('You have not added any service!')

def displayCart(): #display menu for the day
if len(order) == 0:
input('You have not added any service! Press enter to continue...')
return
print(f"Services you have added today: ")
for i, item in enumerate(order,1):
print(f"{i}. {item:20}:\t${menu[item]}k")
choice = 1
while (choice)!=0:
choice=eval(input(f"Enter the service 1-{i} that you would like to remove, or 0 to stop: "))
if choice>=0 and choice<=i:
if choice>0:
print(f"{order.pop(choice-1)} removed")
return
print(order)
else:
print(f"Invalid choice! Please enter 1-{i} or 0 to stop")

print()
input("Press any key to continue...")

def search():
choice= " "

while len(choice)>0:
choice = input("\n\nPlease input service to search: ")
if len(choice)==0:
break

matches=[]

for service in menu.keys():
if service.lower().find(choice.lower())>=0:
matches.append(service.strip())

if len(matches)==0:
print(f"Sorry we don't have services: {choice}!\n")
choice=input("Please press ENTER key to quit or 'N' enter new search: ")
else:
addOrder(matches)


def addOrder(matches):
submenu=""
for i,service in enumerate(matches,1):
submenu+=f"\n{i}. {service:20}:\t${menu[service]}k/year"

print(f"yes, we have the following services(s):{submenu}")
choice=-1
while (choice)!=0:
choice=0 #indicate exit case
choice=eval(input(f"Enter the service 1-{i} that you would like to add, or 0 to stop: "))
if choice>=0 and choice<=i:
if choice>0:
order.append(matches[choice-1])
print(f"{choice} added")
#print(order)
else:
print(f"Invalid choice! Please enter 1-{i} or 0 to stop")

print(f"You have added: {order}")

order=[] #cart
menu={} #menu
if __name__ == '__main__':
init() #populate menu
main() #display welcome screen
Reply
#6
In line 103, you reference the variable i which at that point is undefined. It was discarded once the for loop was complete
imbacoconut likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a menu and execute a function based on user selection in python? Thedarkphoenix 1 1,288 Nov-23-2022, 07:53 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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