Python 调用函数的用户输入

Python 调用函数的用户输入,python,python-3.x,input,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 ent

我试图让用户输入调用一个函数

我的代码:

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")

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

现在它可以按您的需要工作了

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();

在这段代码中,变量theu route是一个字符串,因为输入函数返回一个字符串。你可以试试

    if int(the_route) == 1:

在调用它们之前,必须先定义
第一个
第二个
input
返回一个字符串,该字符串不是感谢的整数可能重复项,@Chris_Rands,我将检查该答案。谢谢你,我所做的是
如果路径==str(1)
,这项工作是由谁完成的。你的代码是否更健壮?你的代码的问题是,它抛出了一个错误,抱怨“first”和“second”在调用时没有定义。是的,我还更改了顺序:)我实际上也需要在你的代码上使用
str