关键字加上字典中的函数
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:
所以,这个程序做了我想让它做的一切,但问题是它对我来说有点麻烦。如果我想访问 "Information",我必须输入 "Information" 并且我不能偏离它,因为系统不会识别该条目。
我想让它能识别“0”或"Information";用户输入的模糊匹配作为正确的输入会更好。
关于我如何做到这一点有什么建议吗?
我们可以使用带有输入数字和函数的字典,而不是做复杂的事情。即:
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 中使用小写字母并将用户输入转换为小写字母来使其更加用户友好。
我想我找到了一个不错的解决方案,其中包括输入验证。您可以轻松地将其变成通用函数。
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()
我已经为我的程序创建了一个菜单系统。我将函数保存在另一个我没有在这里引用的文件中。
这是我的代码:
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:
所以,这个程序做了我想让它做的一切,但问题是它对我来说有点麻烦。如果我想访问 "Information",我必须输入 "Information" 并且我不能偏离它,因为系统不会识别该条目。
我想让它能识别“0”或"Information";用户输入的模糊匹配作为正确的输入会更好。
关于我如何做到这一点有什么建议吗?
我们可以使用带有输入数字和函数的字典,而不是做复杂的事情。即:
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 中使用小写字母并将用户输入转换为小写字母来使其更加用户友好。
我想我找到了一个不错的解决方案,其中包括输入验证。您可以轻松地将其变成通用函数。
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()