0
def sort(a,n):
    for i in range(0,n):
        for j in range(i+1):
            if a[i]<a[j]:
                t=a[i]
                a[i]=a[j]
                a[j]=t
n=input()
a=[]
i=0
j=0
q=0
for i in range(0,n):
    x=int(raw_input())
    a.append(x)
sort(a,n)
for i in range(0,n-1):
    if a[i+1]==a[i]+1:
        q=q+1
if q==n-1:
    print "YES"

The above is the solution to a Hackerearth problem, being new to python i am facing this NZEC error which bothers me a lot as i got no solution for this error.

Can someone please tell me why am i getting this error and whats the solution for the same

  • but your code runs fine in your system? – void Jun 29 '17 at 06:36
  • YES!!!!! It runs perectly – Harshit Jain Jun 29 '17 at 07:08
  • Please read about [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). You can also use [Python-Tutor](http://www.pythontutor.com/visualize.html#mode=edit) which helps to visualize the execution of the code step-by-step. – Tomerikoo Jan 13 '21 at 10:06
  • Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – Tomerikoo Jan 13 '21 at 10:06

2 Answers2

1

The reason why your code is not working is because of this

n = input()

You are using n again as

for i in range(0,n):

range wants an Int number as input.

Hacker earth will show NZEC regardless of the problem it faces. It is upon you to figure out the problem. One good way is to use Try and Except blocks. Simply adding this will lead to a lot of easier debugging

try:
     YOUR CODE DOING STUFF
except Exception as E:
     print(f"Error occurred::: {E}")
JackX
  • 91
  • 6
0

Well NZEC stands for Non zero exit code error.

You can probably try to put the entire code inside a try..except. Where the except just ignores the exception. Try if this works.

try:
  your_function()
except Exception: 
  pass

So put your entire code inside try..except try this,

try:
  entire_code()
except Exception: 
  pass

Where entire_code() is:

def sort(a,n):
    for i in range(0,n):
        for j in range(i+1):
            if a[i]<a[j]:
                t=a[i]
                a[i]=a[j]
                a[j]=t
def entire_code():
    n=input()
    a=[]
    i=0
    j=0
    q=0
    for i in range(0,n):
        x=int(raw_input())
        a.append(x)
    sort(a,n)
    for i in range(0,n-1):
        if a[i+1]==a[i]+1:
            q=q+1
    if q==n-1:
        print "YES"

Or maybe even try to print the stacktrace in the except block.

void
  • 2,571
  • 2
  • 20
  • 35