python - 用户输入调用函数

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

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

最佳答案

现在这可以如你所愿。

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

关于python - 用户输入调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48502404/

相关文章:

python - pandas 和 numpy 之间 std 的不同结果

python - 如何检测 Python AST 以跟踪存储操作?

javascript - HTML 框格式

python - 如何将 24 小时军事格式时间存储到 TimeField 中的 Django 模型

python - 盈透证券Python API连接缓冲区

python - 继承问题和 "self"引用

python - 如何在 tkinter 中制作闪烁的文本框?

mysql - 将输入数字转换为Mysql十进制

javascript - 错误 : HTML onClick not defined with Node. js、NPM 和 Parcel

python - 重定向到带有 header 的 OAuth URL