在我的python程序中,我定义了多个函数,然后是一个也保存菜单的main函数。菜单是应该显示的第一件事,但程序首先尝试运行主函数之前定义的函数。这导致了许多问题。有什么建议。
#!usr/bin/env python
import operator
saved_string = ''
def remove_letter():
return
def num_compare():
return
def print_string():
print saved_string
return
def calculator():
sign_dict = {"+": operator.add(), "-": operator.sub(), "*": operator.mul(), "&": operator.div()}
num1 = int(raw_input("First number: "))
sign = str(raw_input("Action: "))
num2 = int(raw_input("Second number: "))
print sign_dict[sign] (num1, num2)
return
def accept_store():
global saved_string
saved_string = str(raw_input("Enter string: "))
return
def main():
opt_list = [accept_store(),
calculator(),
print_string(),
num_compare(),
remove_letter()]
while(True):
print "SELLECT OPTIONS:"
print "1\tAccept and Store"
print "2\tCalculator"
print "3\tPrint String"
print "4\tNumber Compare"
print "5\tRemove Letter"
opt_choice = int(raw_input("SELLECTION: "))
opt_choice -= 1
opt_list[opt_choice]()
return
main()
答案 0 :(得分:1)
()
是一个函数调用符号。因此,在opt_list
中,您列出了所有函数调用,而不是函数名称。您必须将其更改为:
opt_list = [fn1, fn2, ...]
然后调用每个函数,如:
for f in opt_list:
f()