简体   繁体   English

使用 runpy 不能运行超过 2 条路径

[英]Can't run more than 2 paths with runpy

I have this code, the jogo==1 and jogo==2 options work fine and open what I want but the rest of the options dont open the.py files, is anything wrong with my code?我有这个代码, jogo==1 和 jogo==2 选项工作正常并打开我想要的,但是选项的 rest 没有打开 .py 文件,我的代码有什么问题吗?

import runpy

def menuPrincipal():

  print("\nMenu\n")

  print("1 - Jogo do Galo")
  print("2 - 4 em linha")
  print("3 - Jogo da gloria")
  print("4 - Jogo da força")
  print("5 - Jogo Minas")
  print("6 - Sair")


while True:
  menuPrincipal()
  jogo = int(input("Escolha um jogo: "))

  if jogo == 1:
     runpy.run_path(path_name='jogoDoGalo.py')
  elif jogo == 2:
       runpy.run_path(path_name='jogo4emLinha.py')
  elif jogo == 3:
       runpy.run_path(path_name='jogoDaGloria.py')
  elif jogo == 4:
     runpy.run_path(path_name='jogoDaForca.py')
  elif jogo == 5:
       runpy.run_path(path_name='jogoMinas.py')
  elif jogo == 6:
       break

This is a mere guess, but there might be code in those files that don't seem to run inside an if __name__ == "__main__": condition.这只是一个猜测,但这些文件中可能有一些代码似乎没有在if __name__ == "__main__":条件中运行。

If that's the case, that code won't be executed when called via runpy because then those files will load into Python under module names different than "__main__" .如果是这种情况,则在通过runpy调用时不会执行该代码,因为这些文件将加载到 Python 中,其模块名称不同于"__main__" Try removing the if __name__ == "__main__": condition.尝试删除if __name__ == "__main__":条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM