This is the problem presented in the question by codechef Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at the input are integers of one or two digits.
Example Input: 1 2 Output: 1 2
I tried approaching this problem by setting up a while loop which would accept all inputs, except 42 and return the same input back to the user. Upon encountering 42 I used the sys.exit() function to exit the program
import sys
number=int()
while number != 42:
number = int(input('enter any number '))
print(number)
if number == 42:
sys.exit("yup that's 42 ")
Instead of getting a successful submission I'm getting an NZEC Error
答案 0 :(得分:0)
通过给出消息退出脚本时,该脚本被视为错误并返回退出代码1
。我通过运行脚本然后执行echo $?
来检查先前的退出代码来对此进行了测试。
您应该先正常打印邮件,然后再打印sys.exit()
。
docs中也提到了它:
如果它是整数,则shell等将零视为“成功终止”,而将任何非零值视为“异常终止”。
如果传递了另一种类型的对象,则None等于传递零,并且将任何其他对象打印到stderr并导致退出代码为1。特别是,sys.exit(“ some error message”)是一个发生错误时退出程序的快速方法。