15
votes

I'm working on making a menu in python that needs to:

  1. Print out a menu with numbered options
  2. Let the user enter a numbered option
  3. Depending on the option number the user picks, run a function specific to that action. For now, your function can just print out that it's being run.
  4. If the user enters in something invalid, it tells the user they did so, and re-display the menu
  5. use a dictionary to store menu options, with the number of the option as the key, and the text to display for that option as the value.
  6. The entire menu system should run inside a loop and keep allowing the user to make choices until they select exit/quit, at which point your program can end.

I'm new to Python, and I can't figure out what I did wrong with the code.

So far this is my code:

ans=True
while ans:
    print (""""
    1.Add a Student
    2.Delete a Student
    3.Look Up Student Record
    4.Exit/Quit
    """")
    ans=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") 
    elif ans !="":
      print("\n Not Valid Choice Try again") 

ANSWERED

This is what he wanted apparently:

menu = {}
menu['1']="Add Student." 
menu['2']="Delete Student."
menu['3']="Find Student"
menu['4']="Exit"
while True: 
  options=menu.keys()
  options.sort()
    for entry in options: 
      print entry, menu[entry]

    selection=raw_input("Please Select:") 
    if selection =='1': 
      print "add" 
    elif selection == '2': 
      print "delete"
    elif selection == '3':
      print "find" 
    elif selection == '4': 
      break
    else: 
      print "Unknown Option Selected!" 
4
ans=input("What would you like to do?") <--- missing that at the end - jramirez
and there are too many quotes on your print string ... you are also not using a dictionary or anything else the assignment requires - Joran Beasley
How many quotes should I have? Like I said, I'm new to Python and have been basically going off of online tutorials. - Jack
else: print("\n Not Valid Choice Try again") you dont need the elif. - jramirez
Also, you need a "break" command to exit your loop, on option 4. - dstromberg

4 Answers

16
votes
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]()
8
votes

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.

5
votes

This should do it. You were missing a ) and you only need """ not 4 of them. Also you don't 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")
2
votes

It looks like you've just finished step 3. Instead of running a function, you just print out a statement. A function is defined in the following way:

def addstudent():
    print("Student Added.")

then called by writing addstudent().

I would recommend using a while loop for your input. You can define the menu option outside the loop, put the print statement inside the loop, and do while(#valid option is not picked), then put the if statements after the while. Or you can do a while loop and continue the loop if a valid option is not selected.

Additionally, a dictionary is defined in the following way:

my_dict = {key:definition,...}