Calling sys.exit
will immediately terminate the execution of the program, like return
does for functions:
import sys
...
print math.sqrt(int(h1)**2 + int(h2)**2)
sys.exit()
Frage
I am trying to make a measurement machine with python, but when I enter the measurements it just goes on with the code, I want it to stop processing once I get a result, here is the code I have done so far:
I need it to stop after "print math.sqrt(int(h1)**2 + int(h2)**2)
"
need = raw_input("What do you need to Use?")
if need == "pythagoras" or "Pythagoras":
pythagoras = raw_input("What side do you Need?")
if pythagoras == "hypotenuse" or "Hypotenuse":
h1 = int(raw_input("Known Side 1"))
h2 = int(raw_input("Known Side 2"))
import math
print math.sqrt(int(h1)**2 + int(h2)**2)
Lösung
Calling sys.exit
will immediately terminate the execution of the program, like return
does for functions:
import sys
...
print math.sqrt(int(h1)**2 + int(h2)**2)
sys.exit()
Andere Tipps
̶N̶o̶t̶ ̶n̶e̶e̶d̶ ̶i̶m̶p̶o̶r̶t̶ ̶s̶y̶s̶,̶ ̶w̶i̶t̶h̶ ̶e̶x̶i̶t̶(̶)̶ ̶c̶a̶n̶ ̶e̶x̶i̶t̶.̶
It's more clean if pass 0 argument (good exit):
sys.exit(0)