【发布时间】:2021-10-03 01:29:38
【问题描述】:
我一直在制作一个简单的库存数据库脚本,该脚本在 CLI 中运行,并根据用户输入使用 if/else 语句。我的问题是在它查询 SQL 数据库后,它会提示用户他们是否想再次进行相同的查询或返回主菜单并选择不同的查询。我可以使用 while 循环和 continue 语句再次运行查询代码,但是当我选择“返回主菜单”选项时,它似乎可以通过显示主菜单选项并提示用户进行选择,但一旦用户输入他们的选择没有效果,并第二次显示菜单,然后用户可以选择不同的查询。我已经尝试过使用 if/else 而不是 while 语句之类的方法,并且尝试了更多,但没有任何结果。
代码如下:
# Main Menu Options
def mainmenu():
print("(1) Add (Brand) New Item to Inventory")
print("(2) Remove Item from Inventory")
print("(3) Update Inventory")
print("(4) Print Current Inventory Report")
print("(5) '99' to quit")
print("_" * 32)
global choice_1
choice_1 = (int(input("Enter Choice:")))
while True:
mainmenu()
# (1)
if choice_1 == 1:
while True:
print("What item would you like to add? (eg. 'keyboards', 'laptops', etc.)")
add_item = (input("Enter Choice:")).casefold()
print("Enter quantity of {}:".format(add_item))
quantity = (int(input()))
# Data Insertion
query = "INSERT into items(devicetype,quantity)VALUES (?,?)"
data = (add_item, quantity)
cursor.execute(query,data)
con.commit()
if(cursor.execute(query,data)):
print("Added {} {} to Inventory successfully.".format(quantity, add_item))
else:
print("Could not add {} to Inventory.".format(add_item))
#cursor.close()
# Main menu options
print()
print("Would you like to add another new item to invenotry or return to main menu?")
print()
print("(1) Return to main menu:")
print("(2) Add another item to inventory:")
print()
main_menu_choice_1 = (int(input("Enter choice here:")))
if main_menu_choice_1 == 1:
mainmenu()
else:
if main_menu_choice_1 == 2:
continue
break
【问题讨论】: