Python 如何将两个代码/程序放在一起?
我不知道如何将这两组代码与一个菜单组合在一起,以便能够选择其中一个运行,类似于以下内容:Python 如何将两个代码/程序放在一起?,python,menu,Python,Menu,我不知道如何将这两组代码与一个菜单组合在一起,以便能够选择其中一个运行,类似于以下内容: Welcome to (etc etc) What would you like to play? All computer generated OR You vs computer? Selection 1 or 2? Please enter your selection: 或者类似的东西。 我对编码非常陌生,所以我不知道该如何处理这个问题。非常感谢所有帮助:) 以下是两个代码(如果有帮助的话): 代
Welcome to (etc etc)
What would you like to play?
All computer generated OR You vs computer?
Selection 1 or 2? Please enter your selection:
或者类似的东西。
我对编码非常陌生,所以我不知道该如何处理这个问题。非常感谢所有帮助:)
以下是两个代码(如果有帮助的话):
代码1(计算机生成)
一个简单快捷的方法是创建一个menu.py,根据用户输入运行code1.py或code2.py
#menu.py
import os
print """
Welcome to (etc etc)
What would you like to play?
All computer generated OR You vs computer?
Selection 1 or 2? Please enter your selection:
"""
choice = raw_input()
if choice == '1':
os.system('code1.py')
elif choice == '2':
os.system('code2.py')
else:
print 'invalid choice'
将menu.py、code1.py和code2.py都放在同一个目录中,然后运行menu.py。一个简单快捷的方法就是创建一个menu.py,根据用户输入运行code1.py或code2.py
#menu.py
import os
print """
Welcome to (etc etc)
What would you like to play?
All computer generated OR You vs computer?
Selection 1 or 2? Please enter your selection:
"""
choice = raw_input()
if choice == '1':
os.system('code1.py')
elif choice == '2':
os.system('code2.py')
else:
print 'invalid choice'
将menu.py、code1.py和code2.py全部放在同一目录中,然后运行menu.py。将您的第一个脚本命名为
script1.py
。现在将此添加到第二个脚本的顶部
import script1
以及第二个脚本末尾的以下代码片段:
print("Welcome! What would you like to play?")
print("All computer generated OR You vs computer?")
print("Selection 1 or 2? Please enter your selection:")
while True:
choice = int(input())
if choice == 1:
script1.main()
elif choice == 2:
playgames()
else:
print("Enter either 1 or 2!\n")
(我使用了
python3
语法,但是将它们转换为python2
应该没有问题。)将您的第一个脚本命名为script1.py
。现在将此添加到第二个脚本的顶部
import script1
以及第二个脚本末尾的以下代码片段:
print("Welcome! What would you like to play?")
print("All computer generated OR You vs computer?")
print("Selection 1 or 2? Please enter your selection:")
while True:
choice = int(input())
if choice == 1:
script1.main()
elif choice == 2:
playgames()
else:
print("Enter either 1 or 2!\n")
(我使用了python3
语法,但是将它们转换为python2
应该没有问题)