这可能是一个非常非常基本的愚蠢问题,但我不知道如何做到这一点;我有一个像这样的菜单(在 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/