我正在创建一个基本的电话簿程序,该程序可以执行操作(添加新的、更新、删除、查找和退出)。我有第一个选项作为添加条目,第二个选项作为删除条目。每次用户完成操作时,都会出现再次选择操作的选项。当用户第二次选择时,它会回到第一个项目,而不是选择;例如1是添加新联系人,2是删除新联系人。用户选择1,添加新联系人被要求选择另一个选项,选择2,但选项1的代码再次运行以添加新联系人而不是删除。
print("Please select an option from the main menu :n")
def print_menu():
print("1 : Add New Entry")
print("2 : Delete Entry")
print("3 : Update Entry")
print("4 : Lookup Number")
print("5 : QUIT")
print()
print_menu()
while 1 == 1:
try:
menuOption = int(input())
break
except:
print("Invalid! Please choose an option between 1 and 5.")
while menuOption not in(1,2,3,4,5):
print("Invalid! Please choose an option between 1 and 5.")
try:
menuOption = int(input())
break
except:
print("Invalid! Please choose an option between 1 and 5.")
###the above works perfect to set menu and restrict entry
phonebook = {}
#while menuOption != 5:
#menuOption = int(input("Enter your selection (1-5): "))
while 1 == 1 :
if menuOption == 1: #
print("nNEW CONTACT")
while True:
name = input("Contact Name : ")
if name.replace(' ','').isalpha():
break
print('Please enter a valid name.')
while True:
try:
number = int(input("Contact Number : "))
if number:
break
except:
print("Please enter a valid number.")
if number in phonebook:
print("Contact already exists. Duplicates are not allowed.n")
else:
phonebook[number] = name
print("Success! New contact has been added.n")
print("PLEASE SELECT AN OPTION BETWEEN 1 AND 5 n")
try:
option = int(input())
except:
print("Please enter a numeric value between 1 and 5 n")
elif menuOption == 2: ##delete
print("nDELETE CONTACT")
name = input("Contact Name : ")
if name in phonebook:
del phonebook[name]
print(name, "has been removed from your contacts.")
else:
print("Contact not found.")
print("PLEASE SELECT AN OPTION BETWEEN 1 AND 5 n")
try:
option = int(input())
except:
print("Please enter a numeric value between 1 and 5 n")
欢迎来到堆栈,rainyday!在查看/运行代码时,提醒用户输入1到5之间的信息比我预期的要多,还有其他随机的错误,你可能还没有为此编码。我建议定义更多的函数(用于菜单选项)并对代码进行更多的结构化,这将使代码更易于阅读和遵循。
下面(这不是完整的或无错误的btw),我重新构造了您的代码,以便在调用main()
时,显示"电话簿"菜单选项,用户可以选择另一个选项。不同的功能之间不使用长的"else-if"/elif s,而是将不同的菜单例程整齐地组织在main()
功能中的一个while
语句中,并将选项组织到5个不同的功能中:add()
/delete()
等。(我在更新/查找/退出fns中放入了伪代码)。我希望你觉得这很有帮助。我确实发现,如果我在输入数字时输入了一个字符串,它会抛出一条错误消息。我插入评论以提供帮助。
希望这能帮助
phonebook= []
def main():
print("ntPhone Book") #title
# main menu
print("ntMain Menu")
print("t1. Add a contact")
print("t2. Delete a contact")
print("t3. Update a contact")
print("t4. Look up")
print("t5. Quit")
menuOption = int(input("nPlease select one of the five options "))
while menuOption not in(1,2,3,4,5) :
## insert try/except error handling needed here to handle NaN ##
print("nPlease insert a numeric option between 1 and 5")
menuOption =int(input())
while menuOption <=5:
mOpt =menuOption
if menuOption == 1:
Add(mOpt)
elif menuOption == 2:
Delete(mOpt)
elif menuOption == 3:
Update(mOpt)
elif menuOption == 4:
LookUp(mOpt)
elif menuOption == 5:
ExitPhone(mOpt)
else:
print("Invalid input! Please insert a value between 1 and 5")
# add contact
def Add(mOpt):
##Option 1
add = ""
contact = True
print("ntADD CONTACT")
while contact == True:
if mOpt == 1:
print("nNEW CONTACT")
while True:
name = input("Contact Name : ")
if name.replace(' ','').isalpha():
break
print('Please enter a valid name.')
while True:
try:
number = int(input("Contact Number : "))
if number:
break
except:
print("Please enter a valid number.")
if number in phonebook:
print("Contact already exists. Duplicates are not allowed.n")
else:
#item = name + number this won't be found in the delete function
phonebook.append(name)
phonebook.append(number)
#print(phonebook)
print("Success! New contact has been added.n")
add = input("Would you like to add another? Y (yes) or N (no)")
add = add.lower()
if add=="yes" or add=="y":
contact = True
else:
contact = False
main()
# delete
def Delete(mOpt):
redel = ""
delcontact = True
print("ntDELETE CONTACT")
while delcontact == True:
if mOpt == 2:
print("Enter Contact Name:")
name = input("Contact Name : ")
if name in phonebook:
## insert code here to find associated number
## and concatenate it if you have created an 'item'
phonebook.remove(name) #removes name, leaves the number
print(name, "has been removed from your contacts.")
#print(phonebook)
else:
print("Contact not found.")
redel = input("Would you like to delete another? Y (yes) or N (no)")
redel = redel.lower()
if redel =="yes" or redel =="y":
delcontact = False
else:
delcontact = True
main()
def Update(mOpt):
if mOpt == 3:
print("nUpdate function")
main()
def LookUp(mOpt):
if mOpt == 4:
print("nLookUp function")
main()
def ExitPhone(mOpt):
if mOpt == 5:
print ("Exit?")
main()
main()
您的代码检查menuOption
的值,但输入option
。只需更改
option = int(input())
进入
menuOption = int(input())