2016-08-30 11 views
0

Ich bin sehr neu in Python und will wissen, wie ein Programm mit einer if-Anweisung zu beenden, die eine Meldung ausgibt, da dies nicht mit meinem ‚stehen‘ Variable des Fall zu sein scheint, wenn es unter < 5000. Die if-Anweisung fett ** ** ist der Code, mit dem ich Probleme habe, und es druckt immer noch die Nachricht, die ich will, aber das Programm nicht mit dieser Nachricht zu stoppen, sondern mit dem nächsten Code (Countdown-Variable)). Ignoriere 'Countdown'.beenden Skript mit if-Anweisung

Hier ist ein Teil meines Codes.

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: 

Antwort

1

Verwenden Sie break, um die Schleife zu beenden.

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 

Verwenden Sie , um das Programm zu beenden.

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

Sie können Ihren Code in Funktion setzen. Eine Funktion können Sie die verschiedenen Werte zurückgeben. Während Sie die Funktion aufrufen, können Sie überprüfen, welche Funktion zurückkehrt. Sie können wie wenn die Bedingung verwenden

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

Wir break-Anweisung innerhalb if-Anweisung für die Beendigung verwenden können. Statt Pause können wir os.exit oder sys.exit

schreiben os._exit die C-Funktion _exit() aufruft, die eine sofortige Beendigung des Programms tut. Beachten Sie die Aussage "Kann nie zurückkehren".

sys.exit() identisch Systemexit zu erhöhen(). Es löst eine Python-Ausnahme aus, die vom Aufrufer abgefangen werden kann.

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) 
Verwandte Themen