Creating a Menu in Python

Creating a Menu in Python

def my_add_fn():
   print SUM:%s%sum(map(int,raw_input(Enter 2 numbers seperated by a space).split()))

def my_quit_fn():
   raise SystemExit

def invalid():
   print INVALID CHOICE!

menu = {1:(Sum,my_add_fn),
        2:(Quit,my_quit_fn)
       }
for key in sorted(menu.keys()):
     print key+: + menu[key][0]

ans = raw_input(Make A Choice)
menu.get(ans,[None,invalid])[1]()

There were just a couple of minor amendments required:

ans=True
while ans:
    print (
    1.Add a Student
    2.Delete a Student
    3.Look Up Student Record
    4.Exit/Quit
    )
    ans=raw_input(What would you like to do? ) 
    if ans==1: 
      print(n Student Added) 
    elif ans==2:
      print(n Student Deleted) 
    elif ans==3:
      print(n Student Record Found) 
    elif ans==4:
      print(n Goodbye) 
    elif ans !=:
      print(n Not Valid Choice Try again) 

I have changed the four quotes to three (this is the number required for multiline quotes), added a closing bracket after What would you like to do? and changed input to raw_input.

Creating a Menu in Python

This should do it. You were missing a ) and you only need not 4 of them. Also you dont need a elif at the end.

ans=True
while ans:
    print(
    1.Add a Student
    2.Delete a Student
    3.Look Up Student Record
    4.Exit/Quit
    )
    ans=raw_input(What would you like to do? )
    if ans==1:
      print(nStudent Added)
    elif ans==2:
      print(n Student Deleted)
    elif ans==3:
      print(n Student Record Found)
    elif ans==4:
      print(n Goodbye) 
      ans = None
    else:
       print(n Not Valid Choice Try again)

Leave a Reply

Your email address will not be published. Required fields are marked *