python - 将模块启动到 python 菜单中

标签 python function menu import launch

这可能是一个非常非常基本的愚蠢问题,但我不知道如何做到这一点;我有一个像这样的菜单(在 Python 3 中):

boucle = True
while boucle:
    print('''
        1−Gestion de listes
        2−Gestion de matrices
        0-Quitter
    ''')

    choix = input('Choissisez une commande:')
    if choix == '1':
        gestionliste() 
    elif choix == '2':
        print('Gestions de matrices')
    elif choix == '0':
        boucle = False
    else:
        print('Entrée erronée:Veuillez entrez loption 1,2 ou 0')

(是的,顺便说一下,它是用法语写的),我希望当用户输入“1”作为选择时,我想让它启动我在同一个 .py 文件中创建的函数,例如 def thefunction():

我希望菜单在用户输入“1”时启动 thefunction() 函数。我尝试了很多东西,例如(在if choix=='1'之后)function()、import function()、from file.py import()...但没有任何效果。我猜我还没有找到正确的方法?

最佳答案

您收到什么错误?该代码可以自行运行。

whatever = True

def thefunc():
    print("Works!")

while whatever == True:

    print("""
        1-Whatever
        2-Whatever
        3-Whatever
    """)

    choice = input("Choice: ")

    if choice == "1":
        thefunc()

    elif choice == "2":
        print("...")

    elif choice == "0":
         whatever = False

    else:
        print("... again")

只要您在调用函数之前的某个时刻声明了该函数,您的代码就应该可以工作。您的代码没有任何问题,但请确保您的函数已正确声明。

干杯,

关于python - 将模块启动到 python 菜单中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22750934/

相关文章:

python - 无法将 16 位数字转换为人类可读的日期

css - 自定义页面类未显示在代码中

html - 为什么对齐 ="right"不起作用?

python - 在 OpenCV 中检测(并拒绝)从计算机/电视屏幕拍摄的扭曲图像

python - 如何将新的 "Conn Types"添加到 Airflow 2.0?

c - 我如何从C调用以self作为参数的lua函数?

javascript - 如何根据变量值禁用按钮?

html - 网站的外部菜单

python - 为每个类别python选择前10条记录

C++ 函数返回 vs 函数设置指针