繁体   English   中英

Python main()无法正确执行

[英]Python main() not executing properly

在我的python程序中,我定义了多个函数,然后是一个还包含菜单的主函数。 菜单是应该显示的第一件事,但是程序首先尝试运行在主功能之前的已定义功能。 这导致许多问题。 有什么建议么。

#!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()

()是函数调用符号。 因此,在opt_list ,您列出了所有函数调用 ,而不是函数名称。 您必须将其更改为:

opt_list = [fn1, fn2, ...]

然后像这样调用每个函数:

for f in opt_list:
    f()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM