0

我正在阅读Core Python Programming一书。练习 2_11 指示构建一个菜单系统,该系统允许用户选择运行早期简单程序的选项。菜单系统将保持打开状态,直到用户选择退出选项。这是我的第一个工作程序。

programList = {1:"menu system", 
    2:"for loop count 0 to 10", 
    3:"positive or negative", 
    4:"print a string, one character at a time", 
    5:"sum of a fixed tuple", 
    "x":"quit()", 
    "menu":"refresh the menu"}
import os
for x in programList:
    print(x,":",programList[x])

while True:
    selection = input("select a program: ")
    if selection == "1":
        os.startfile("cpp2_11.py")

    elif selection == "2":
        os.startfile("cpp2_5b.py")

    elif selection == "3":
        os.startfile("cpp2_6.py")

    elif selection == "4":
        os.startfile("cpp2_7.py")

    elif selection == "5":
        os.startfile("cpp2_8.py")

    elif selection == "menu":
        for x in range(8): print(" ")
        for x in programList:print(x,":",programList[x])

    elif selection == "X":
        break

    elif selection == "x":
        break

    else:
        print("not sure what you want")
input()
quit()

这个版本运行良好,但我想使用字典作为 case/switch 语句来清理丑陋的 if/elif/else 行。

现在我被困住了。我将 Eclipse 与 PyDev 一起使用,我的新代码抛出错误:

重复签名:!!

这是我当前代码的副本:

import os

def '1'():
    os.startfile("cpp2_11.py")
def '2'():
    os.startfile("cpp2_5b.py")
def '3'():
    os.startfile("cpp2_6.py")
def '4'():
    os.startfile("cpp2_7.py")
def '5'():
    os.startfile("cpp2_8.py")
def 'm'():       
    for x in range(8): print(" ")
    for x in actions:print(x,":",actions[x])
def 'x'():
    quit()
def errhandler():    
    else:
        print("not sure what you want")


actions = {1:"menu system",
    2:"for loop count 0 to 10", 
    3:"positive or negative", 
    4:"print a string, one character at a time", 
    5:"sum of a fixed tuple", 
    "X":"quit()", 
    "menu":"refresh the menu"}

for x in actions:
    print(x,":",actions[x])

selectedaction = input("please select an option from the list")
while True:
    actions.get(selectedaction,errhandler)()
input()
quit()

我很确定我当前的问题(错误代码)与我尝试在函数中使用 os.startfile() 的方式有关。也许我已经走远了。任何帮助表示赞赏。

编辑:我正在更改标题以使其对将来的参考更有用。在 Ryan 的有用评论指出函数命名中的简单错误之后,我能够拼凑出一个有效的脚本。有点......这里是:

import os

def menu_system():
    os.startfile("cpp2_11alt.py")
def loopCount_zero_to_ten():
    os.startfile("cpp2_5b.py")
def positive_or_negative():
    os.startfile("cpp2_6.py")
def print_a_string_one_character_at_a_time():
    os.startfile("cpp2_7.py")
def sum_of_a_tuples_values():
    os.startfile("cpp2_8.py")
def refresh_the_menu():       
    for x in range(4): print(" ")
    for y in actions:print(y,":",actions[y])
    for z in range(2): print(" ")
def exit_the_program():
    quit()
def errhandler():    
    print("not sure what you want")

actions = {'1':menu_system,
    '2':loopCount_zero_to_ten, 
    '3':positive_or_negative, 
    '4':print_a_string_one_character_at_a_time, 
    '5':sum_of_a_tuples_values, 
    'x':exit_the_program, 
    'm':refresh_the_menu}

for item in actions:
    print(item,":",actions[item])
for z in range(2): print(" ")    

selectedaction = input("please select an option from the list:    ")
while True:
    actions.get(selectedaction,errhandler)()
    selectedaction = input("please select an option from the list:    ")
quit()

第二次尝试有很多问题。我在调用函数时引用了字典键而不是值。我在菜单打印和处理输入值的方式上也有一些错误。现在我需要做的就是弄清楚如何在没有所有额外信息的情况下打印字典值:这是我打印菜单时的输出:

2 : <function loopCount_zero_to_ten at 0x027FDA08>
3 : <function positive_or_negative at 0x027FD810>
1 : <function menu_system at 0x027FD978>
4 : <function print_a_string_one_character_at_a_time at 0x027FD930>
5 : <function sum_of_a_tuples_values at 0x027FD780>
x : <function exit_the_program at 0x027FD858>
m : <function refresh_the_menu at 0x027FD7C8>

以及如何让菜单按数字顺序打印

再次感谢任何帮助。

4

1 回答 1

0

我终于找到了对字典进行排序并将函数名称打印为字符串的问题的解决方案。在已编辑问题的最后一部分(第 3 代码部分)中,我为开始这篇文章的问题提供了固定代码:如何在字典中使用整数来创建菜单 - 旨在创建开关/案例样式替代方案并避免在第一个代码部分出现丑陋的 if/elif/else 问题。

这是工作代码的最终版本:

导入操作系统

def menu_system():
    os.startfile("cpp2_11alt.py")
def loopCount_zero_to_ten():
    os.startfile("cpp2_5b.py")
def positive_or_negative():
    os.startfile("cpp2_6.py")
def print_a_string_one_character_at_a_time():
    os.startfile("cpp2_7.py")
def sum_of_a_tuples_values():
    os.startfile("cpp2_8.py")
def refresh_the_menu():       
    for x in range(4): print(" ")
    for key in sorted(actions):
        print (key, '=>', actions[key].__name__)
    for z in range(2): print(" ")
def exit_the_program():
    quit()
def errhandler():    
    print("not sure what you want")

actions = {'1':menu_system,
    '2':loopCount_zero_to_ten, 
    '3':positive_or_negative, 
    '4':print_a_string_one_character_at_a_time, 
    '5':sum_of_a_tuples_values, 
    'x':exit_the_program, 
    'm':refresh_the_menu}

for key in sorted(actions):
    print (key, '=>', actions[key].__name__)

selectedaction = input("please select an option from the list:    ")
while True:
    actions.get(selectedaction,errhandler)()
    selectedaction = input("please select an option from the list:    ")
quit()

添加该.__name__方法允许我将函数名称打印为字符串。

使用 for 循环:

 for key in sorted(actions):
        print (key, '=>', actions[key].__name__)

创造了对字典进行排序的能力。

于 2013-04-25T23:40:13.283 回答