I'm working on making a menu in python that needs to:
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!"
Python exit commands: quit(), exit(), sys. exit() and os. _exit()
6: user_input = int(input()) if user_input == 1: print(Executing menu item 1) elif user_input == 2: print(Executing menu item 2) elif user_input == 3: print(Executing menu item 3) elif user_input == 4: print(Executing menu item 4) elif user_input == 5: print(Executing menu item 5) elif user_input == 6: print(Exiting... ...
Method 1: Clear screen in Python using cls You can simply “cls” to clear the screen in windows.
Python provides a multiple-choice input system. You have to first get the keyboard input by calling the input() function. Then evaluate the choice by using the if-elif-else structure.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With