问题描述
我正在通过核心Python编程这本书。练习2_11指示建立一个允许用户选择的菜单系统,并选择运行一个较早的简单程序的选项。菜单系统将保持打开,直到用户选择退出选项。这是我的第一个工作程序。 programList = {1:menu system,
2:for loop计数0到10,
3:正或负,
4:打印一个字符串,一次一个字符,
5:固定元组的总和
x:quit(),
菜单:刷新菜单
import os
在程序列表中的x:
print ,:,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(不确定你想要什么)
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 range(8):print()
for x :print(x,:,actions [x])
def'x'():
quit()
def errhandler():
else:
打印(不确定你想要什么)
actions = {1:菜单系统,
2:循环计数为0到10,
3:正或负,
4:打印一个字符串,一次一个字符,
5:固定元组的总和,
X: quit(),
menu:刷新菜单}
for x in action:
print(x,:,actions [x])
selectedaction = input(请从列表中选择一个选项)
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 。
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( 不确定你想要什么)
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}
对于项目中的操作:
print(item,:,actions [item])
for z in range(2):print()
selectedaction = input(请从列表中选择一个选项)
while True:
actions.get(selectedaction,errhandler)()
selectedaction = input(请选择一个选项从列表:)
退出()
第二次尝试有很多问题。在调用函数时,我引用了字典键而不是值。菜单打印和处理输入值的方式也有一些错误。现在我需要做的就是弄清楚如何获取字典值,无需提供所有额外的信息:
这是打印菜单时的输出:
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>
AND 如何让菜单以数字顺序打印。
再次感谢任何帮助。
找到了排序字典并将函数名称打印为字符串的问题的解决方案。在编辑的问题的最后部分(第3代码部分)中,我有一个固定的代码来启动这个帖子:如何在字典中使用整数来创建一个菜单 - 旨在创建一个开关/案例样式替代并避免在第一个代码段中出现丑陋的if / elif / else问题。
这是工作代码的最终版本:
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()
用于排序(动作)中的键:
print(key,'=>',actions [key] .__ name__ )
for z in range(2):print()
def exit_the_program():
quit()
def errhandler():
print(不确定你想要什么)
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}
用于排序(动作)中的键:
print(key,'=>',actions [key] .__ name__)
selectedaction = input(请从列表中选择一个选项)
while True:
actions.get(selectedaction,errhandler)()
selectedaction = input (请从列表中选择一个选项)
quit()
添加 .__名称__
方法允许我以字符串形式打印函数名。
使用for循环:
对于排序(动作)中的键:
print(key,'=>',acti ons [key] .__ name__)
创建排序字典的功能。
I am working my way through the book Core Python Programming. Exercise 2_11 instructed to build a menu system that had allowed users to select and option which would run an earlier simple program. the menu system would stay open until the user selected the option to quit. Here is my first working program.
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()
This version worked fine, but I wanted to use the a dictionary as a case/switch statement to clean up the ugly if/elif/else lines.
Now I'm stuck. I'm using Eclipse with PyDev and my new code is throwing an error:
Duplicated signature:!!
Here's a copy of my current code:
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()
I'm pretty sure that my current problem (the error codes) are related to the way I'm trying to use the os.startfile() in the functions. Maybe I'm way off. Any help is appreciated.
EDIT: I am changing the title to make it more useful for future reference. After a helpful comment from Ryan pointing out the simple error in function naming, I was able to piece together a script that works. sort of...Here it is:
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()
There were many problems with the second attempt. I was referencing the dictionary key instead of the value when calling functions. I also had some bugs in the way the menu printed and handled input values. Now all I need to do is figure out how to get the dictionary values to print without all of the extra information:This is the output when I print the menu:
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>
AND how to get the menu to print in numeric order.
Once again, any help is appreciated.
I finally found a solution to the problem of sorting a dictionary and printing the function names as a string. In the last part of the edited question (3rd code section), I had the fixed code for the question that started this post: how to use integers in a dictionary to create a menu - with the intention of creating a switch/case style alternative and avoiding the ugly if/elif/else problems in the first code section.
Here's the final version of the working code:
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 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()
adding the .__name__
method allowed me to print the function names as a string.
Using the for loop:
for key in sorted(actions):
print (key, '=>', actions[key].__name__)
created the ability to sort the dictionary.
这篇关于使用整数与字典创建文本菜单(开关/案例替代)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!