2016-05-27 10 views
0

他のプログラム(私が前に作ったゲーム)を開き、ウィンドウが即座に閉じるプログラムを作ろうとしています。'ゲーム'をインポートしてウィンドウを閉じる

#Program that runs game i have made 
import subprocess 

choice = input("What would you like to do? \nGuess my number game (1) \nCalorie counter (2) \nWord jumble game (3) \nInsert your decision here - ") 

while choice == "1": 
    print("Let us begin") 
    def start_guess_my_number(): 
     subprocess.call(['python', 'Guess my number game2.py']) 
    start_guess_my_number() 
    choice = input("What would you like to do now? 1 2 or 3 ? - ") 
while choice == "2": 
    print("Let us begin") 
    def start_calorie_counter(): 
     subprocess.call(['python', 'Calorie counter.py']) 
    start_calorie_counter() 
    choice = input("What would you like to do now? 1 2 or 3 ? - ") 
while choice == "3": 
    print("Let us begin") 
    def start_guess_my_number(): 
     subprocess.call(['python', 'Word jumble game.py']) 
    start_guess_my_number() 
    choice = input("What would you like to do now? 1 2 or 3 ? - ") 
input("Press enter to exit") 

注:私は、私は呼びかけていたプログラムが動作していることを確認してきた、と黒のコマンドウィンドウで開いたときに、彼らは私がこのプログラムを経由して、それらを開いたときとは違って、開いたまま。

+0

端末を使用してプログラム(python app.py)を実行すると、出力が表示されることがあります –

答えて

4

あなた持っていた次のような問題:あなたはintに入力を比較する必要が

  • 、文字列ではありませんあなたは新しいプロセスが実行されているデフォルトでは、右のフォルダにゲームを開く必要があり
  • python実行可能ファイルを含むフォルダ。
  • if文の代わりにwhileループを使用していたため、コードは無限ループで捕捉されていました。
  • メインループから抜け出す方法はありませんでした。そのためにはbreak文が必要です。

私もコードを機能に分けました。

#Program that runs a game I have made 
import os 
import subprocess 


def play_game(name): 
    print("Let us begin") 
    subprocess.call(['python', os.path.dirname(os.path.realpath(__file__))+os.path.sep+name]) 


choice = input("What would you like to do? \nGuess my number game (1) \nCalorie counter (2) \nWord jumble game (3) \nInsert your decision here - ") 

while True: 
    if choice == 1: 
     play_game('Guess my number game2.py') 
    elif choice == 2: 
     play_game('Calorie counter.py') 
    elif choice == 3: 
     play_game('Word jumble game.py') 
    else: 
     break 
    choice = input("What would you like to do now? 1 2 or 3 ? - ") 
print("Goodbye") 
関連する問題