用户输入调用功能

时间:2018-01-29 13:28:05

标签: python python-3.x input

我正在尝试让用户输入调用函数。

我的代码:

class the_proccess():

    the_route = input("Which function do you want to use? \n\n 1) The first one. \n\n 2) The second one. \n\n 3) The first one and then the second one. \n\n Please enter the corresponding number and hit enter >>>>> ")

    if the_route == 1:
        first()
    elif the_route == 2:
        second()
    elif the_route == 3:
        first()
        second()

    def first():
        print("First function")

    def second():
        print("Second function")

但是,当我选择该选项时,该过程将停止。我不太确定我做错了什么,这是我第一次尝试做这样的事情。

2 个答案:

答案 0 :(得分:2)

现在这可以按照你的意愿工作。

class the_proccess(object):

def first(self):
    print("First function")

def second(self):
    print("Second function")

def __init__(self):
    self.the_route = input("Which function do you want to use? \n\n 1) The first one. \n\n 2) The second one. \n\n 3) The first one and then the second one. \n\n Please enter the corresponding number and hit enter >>>>> ")

    if self.the_route == 1:
            self.first()
    elif self.the_route == 2:
            self.second()
    elif self.the_route == 3:
            self.first()
            self.second()

a = the_proccess();

答案 1 :(得分:0)

在此代码中,变量the_route是一个字符串,因为输入函数返回一个字符串。您可以尝试

    if int(the_route) == 1: