2016-08-30 13 views
0

私はPythonを初めて使い、メッセージを出力するif文でプログラムを終了する方法を知りたいのですが、これは私の 'stand'変数で起きているようには見えません<の下にあります。**ステートメントは太字で表示されています** **は私が問題を抱えているコードですが、それでも私が望むメッセージを表示しますが、プログラムはそのメッセージで停止しませんが、 )。 「カウントダウン」を無視する。if文を使ったスクリプトの終了

ここは私のコードの一部です。

while stand <= 0 or mission > 10000: 
     try: 
      stand = int(input("Enter a VALID distance in metres: ")) 
     except ValueError: 
      print("Please enter a valid distance E.g 6000: ") 
    **if stand > 0 or stand < 5000: 
     print("ERROR, ERROR") 
     print("Launch cannot begin, the Mission Control and spectator stands are dangerously close at a distance of {}m.".format(mission)) 
     print("This launch site sucks! It must be demolished and rebuilt!") 
     print("Launch delayed.")**     

    if stand >= 5000: 
     print("Fortunately the mission control and viewing stands are situated far enough.")    
    while countdown == 0 or countdown == "": 
     print("We need a person to countdown.") 
     try: 
      countdown = int(input("How many seconds would you like the countdown to be?: ")) 
     except ValueError: 

答えて

1

ループを終了するには、breakを使用します。

if stand > 0 or stand < 5000: 
    print("ERROR, ERROR") 
    print("Launch cannot begin, the Mission Control and spectator stands are dangerously close at a distance of {}m.".format(mission)) 
    break 

exitを使用してプログラムを終了します。

import sys 
if stand > 0 or stand < 5000: 
    print("ERROR, ERROR") 
    print("Launch cannot begin, the Mission Control and spectator stands are dangerously close at a distance of {}m.".format(mission)) 
    sys.exit(0) 
0

コードを機能させることができます。関数は異なる値を返すことができます。関数を呼び出すと、どの関数が返されているかを調べることができます。

def func1(): 
     while stand <= 0 or mission > 10000: 
      try: 
       stand = int(input("Enter a VALID distance in metres: ")) 
      except ValueError: 
       print("Please enter a valid distance E.g 6000: ") 
     **if stand > 0 or stand < 5000: 
      print("ERROR, ERROR") 
      print("Launch cannot begin, the Mission Control and spectator stands are dangerously close at a distance of {}m.".format(mission)) 
      print("This launch site sucks! It must be demolished and rebuilt!") 
      print("Launch delayed.") 
      return 1**     

     if stand >= 5000: 
      print("Fortunately the mission control and viewing stands are situated far enough.")    
     while countdown == 0 or countdown == "": 
      print("We need a person to countdown.") 
      try: 
       countdown = int(input("How many seconds would you like the countdown to be?: ")) 
      except ValueError: 
r = func1() 
if r == 1: 
    print 'Error' 
0

などの条件は我々が終了するのif文の内側にbreak文を使用できるかどう あなたは使用することができます。中断するのではなく、os.exitまたはsys.exitを書くことができます。

os._exitは、プログラムの即時終了を行うC関数_exit()を呼び出します。 「can not return」というステートメントに注意してください。

sys.exit()は、raise SystemExit()と同じです。 Pythonの例外が発生し、呼び出し側が捕まえる可能性があります。

import os 

if stand > 0 or stand < 5000: 
    print("ERROR, ERROR") 
    print("Launch cannot begin, the Mission Control and spectator stands are dangerously close at a distance of {}m.".format(mission)) 
    os.exit(0) 
関連する問題