[英]Keyword coupled with a function in a dictionary
我為我的程序創建了一個菜單系統。 我將函數保存在另一個文件中,但我沒有在此處引用。
這是我的代碼:
print("\nHauptmenü\n")
hauptmenue = {"Information":programm_information,
"Beenden": programm_beenden,
"Hilfe":programm_erklaerung,
"Trennzeichen":text_trennzeichen,
"Lesen":csv_suchanfrage,
"csv_suche":csv_suchanfrage}
while True:
for menue_punkt in enumerate (hauptmenue):
print(menue_punkt)
eingabe = input("\nOption: bitte einen Menüpunkt eingeben: ")
args = eingabe.split()
if len(args) < 4:
if args[0] in hauptmenue:
key = args[0]
hauptmenue[key]()
else:
print (eingabe," ist keine gültige Option.\n ")
print("Hauptmenü\n")
輸出:
Hauptmenü
(0, 'Information')
(1, 'Beenden')
(2, 'Hilfe')
(3, 'Trennzeichen')
(4, 'Lesen')
(5, 'csv_suche')
Option: bitte einen Menüpunkt eingeben: Information
Version : 1.0
Datum der letzten Version : 04.01.2020
(0, 'Information')
(1, 'Beenden')
(2, 'Hilfe')
(3, 'Trennzeichen')
(4, 'Lesen')
(5, 'csv_suche')
Option: bitte einen Menüpunkt eingeben:
所以,程序做了我想要它做的一切,但問題是它對我來說有點麻煩。 如果我想訪問“信息”,我必須輸入“信息”,我不能偏離它,因為系統不會識別該條目。
我想讓它可以識別“0”或“信息”; 用戶條目的模糊匹配作為正確條目會更好。
關於我如何做到這一點的任何建議?
我想我找到了一個不錯的解決方案,其中包括輸入驗證。 你可以很容易地把它變成一個通用函數。
def programm_information():
return None
def programm_beenden():
return None
def programm_erklaerung():
return None
def text_trennzeichen():
return None
def csv_suchanfrage():
return None
menu_options_dict = {"Information": programm_information,
"Beenden": programm_beenden,
"Hilfe": programm_erklaerung,
"Trennzeichen": text_trennzeichen,
"Lesen": csv_suchanfrage,
"csv_suche": csv_suchanfrage}
invalid_input_msg = 'Invalid choice, please try again. Press ENTER to continue.'
while True:
print('Choose an option:')
for num, elem in enumerate(menu_options_dict, start=1):
print(f'{num}: {elem}')
choice_str = input('Option: bitte einen Menüpunkt eingeben: ').strip()
options_dict_res = menu_options_dict.get(choice_str)
if options_dict_res:
break
else:
try:
choice_num = int(choice_str)
except ValueError:
input(invalid_input_msg)
else:
if 0 < choice_num <= len(menu_options_dict):
options_dict_res = list(menu_options_dict.values())[choice_num - 1]
break
else:
input(invalid_input_msg)
print(options_dict_res)
func_res = options_dict_res()
我們可以使用帶有輸入數字和函數的字典,而不是做復雜的事情。 那是:
hauptmenue = {"Information":programm_information,
"Beenden": programm_beenden,
"Hilfe":programm_erklaerung,
"Trennzeichen":text_trennzeichen,
"Lesen":csv_suchanfrage,
"csv_suche":csv_suchanfrage}
inputmenu = {0 : "Information",
1 : "Beenden",
2 : "Hilfe",
3 : "Trennzeichen",
4 : "Lesen",
5 : "csv_suche"}
while True:
for choice, option in enumerate(inputmenu):
print(choice, option)
choice_str = input("\nOption: bitte einen Menüpunkt eingeben: ").strip() # strip removes leading n trailing white spaces.
if choice_str.isalpha():
#Everything is alphabet, so it must an option name.
option = choice_str #Not needed, but writing to make it easy to understand
else:
option = inputmenu.get(int(choice_str), None) # gives None if choice not found.
func = hauptmenue.get(option, False)
if not func: func()
這對於一小組輸入來說是快速和更好的,並且易於維護。 您可以通過在 hauptmenu 中使用小寫字母並將用戶輸入轉換為小寫來使其更加用戶友好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.