def reverse_number(x):
return x[::-1]
number_of_sums = int(input())
for i in range(number_of_sums):
s1 = input().split()
print(int(reverse_number(s1[0])) + int(reverse_number(s1[1])))
I have the following code, which should be a solution to a SPOJ problem. It compiles fine and works for examples that I provide, but as soon as I submit it says that it found an EOF error.
It points me to line 5 in the code, and I believe I understand why. When I use input() I try to get the whole line, right? So, how would I go ahead to only get the number? If that is the problem of course, it might be something else that ruins the code.
def reverse_number(x):
return x[::-1]
try:
number_of_sums = int(input())
for i in range(number_of_sums):
s1 = input().split()
if len(s1) > 0:
print(int(reverse_number(s1[0])) + int(reverse_number(s1[1])))
except ValueError:
print("Invalid Input")
Now, if you run the program and submit it will throw error stating "Invalid Input". Apart from adding the try block, also included the condition check for s1 as you might encounter an "Index out of range" error if not given proper input.
It may be that the grader/engine that checks the code is running Python2, in which case you could change to:
number_of_sums = int(raw_input())
before submitting (and a similar change on line 8).
Related
The following code works fine on both the jupiter notebook and the pycharm but its showing runtime error on codechef. The code is to check whether the permutation is ambiguous or not.
for i in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
p=l[:]
for j in range(n):
p[l[j]-1]=j+1
if(p==l):
print("ambiguous")
else:
print("not ambiguous")
Welcome to Stack Overflow!
When posting, it usually helps to add more information, such as the error message itself. Regardless, I pasted your code into Python and I've deduced that the problem arises from the input() statement- I'm not sure what 'code chef' is, but it probably doesn't know that the user should input an integer into the input() prompt.
Therefore, a workaround is to hardcode your inputs as variables:
input1 = "5"
input2 = ...
for i in range(int(input1)):
n=int(input2)
...
I am a newbie in python, started learning the language a week ago tried to test it out in codechef...
Here is the question link: https://www.codechef.com/problems/COOMILK
So with what i understood so far i typed the following piece of code..
t=int(input())
f=[]
if(t >= 1 and t <= 50):
for each in range(t):
n=int(input())
if(n >= 1 and n <=50):
m=input().split()
for i in range(len(m)):
if(m[i]=='milk'):
q=1
elif(len(m)==1 and m[0]=='cookie'):
q=0
break
elif(m[i]=='cookie' and m[i+1]!='milk'):
q=0
break
f.append(q)
for i in f:
if(i==1):
print("YES")
elif(i==0):
print("NO")
So apart from the n constraint being not checked during the input, what actually showed was a runtime error.
New to this can someone explain it clearly why i am getting the nzec error?
Compiled it on my own and for all the test cases it worked properly but does not compile on code chef.. any help would be appreciated.
What i have gathered from the net is that there is no raw_input in python 3.x that i am using currently, so just a friendly reminder here....
NZEC stands for "Non Zero Exit Code". It occurs when you encounter a runtime error usually index out of bound error.
There is a mistake in the 14th line of your code:
elif (m[i]=='cookie' and m[i+1]!='milk'):
what if i+1 is out of range?
e.g.,
Consider the input to be:
1
3
cookie milk cookie
1 is the number of testcases.
3 is the number of elements in the
list.
'cookie milk cookie' composes the list elements.
When the value of i is 2 and your code's 14th line comes to execution, it will generate an index out of bound error(because m[i+1] i.e. m[3] doesn't exist!) which is a type of runtime error and thus generates NZEC error.
Add this code after the 7th line of your code:
if m[-1]=='cookie':
f.append(0)
continue
You could try using
try:
#working code
except Exception:
pass
Try and catch exception for java code.
Problem
Calculate and print the factorial of a given positive integer. The integer can be as large as 100.
Here's a link to the problem
My effort
I have tried solutions on other compilers, they are working fine on other compilers, but on hackerrank its not working saying compile time error
# Enter your code here. Read input from STDIN. Print output to STDOUT
def fac(n):
return 1 if (n < 1) else n * fac(n-1)
no = int(raw_input())
print fac(no)
Any help would be appreciated
This solution works just fine for Python 2 - I ran your code on Hackerrank and it passed all the test cases.
So, the compilation error is shown if the code is compiled with Python 3.
no = int(raw_input())
NameError: name 'raw_input' is not defined
That's true because raw_input must be replaced with input() in Python 3.
If the code with the correction is executed afterwards then there's another issue:
print fac(no)
^
SyntaxError: invalid syntax
Again, just add parentheses around fac(no) and then the code compiles and passes all the tests:
So, the full code is below:
def fac(n):
return 1 if (n < 1) else n * fac(n-1)
no = int(input())
print (fac(no))
Running this code a few times presents no issues. Upon attempting to show a friend, it doesn't work. It just hangs after the input. It's worked quite a few times before but never again unfortunately.
I've tried rewriting the code in brackets, rewriting the code to a local directory instead of the Google Drive folder I have and I've even tried rewriting from scratch in regular notepad. All this was tried in case some sort of encoding issue had occured. No such luck. I figure something is wrong with the interpreter but I'm not sure how to remedy the situation.
def bin2dec():
bin = []
a = int(input("What number are you converting to binary?: "))
while a > 0:
if a % 2 == 0:
bin.insert(0, 0)
a = a/2
elif a % 2 == 1:
bin.insert(0, 1)
a = a/2-0.5
else:
#repetition
print("Your binary equivalent is:", bin)
repeat = input("Would you like to convert another binary number?: ")
if repeat == "yes":
bin2dec()
bin2dec()
Oh....welp. It seems the problem was actually that I somehow installed two versions of pythons and I guess they had been interfering with each other. Reason I'm not deleting this Q is because I'm sure I'm not the only one who's made this mistake. However, others have probably made this mistake in an effort to ensure compatibility between versions. Bad idea.
For the last half hour I've been trying to figure out what is wrong with this code. It should be very straight forward. I've practically copied it out of the documentation at this point. But no matter what I try I receive a syntax error.
Here's the code:
def addfiles(folder):
foldercont = [os.path.normcase(f) for f in os.listdir(folder)]
for x in foldercont:
if os.path.isfile(x) == True:
files.append(os.path.realpath(x)
if os.path.isdir(x) == True:
addfiles(os.path.realpath(x))
Whenever I run this, i receive the error
if os.path.isdir(x) == True:
^
SyntaxError: invalid syntax
However, if I write the equivlent code in the interactive interpreter it runs fine.
Can this method simply not be used in an if loop or something?
Thanks for the help. I'm getting really frustrated at this point... heh.
There's a parenthesis missing at this line:
files.append(os.path.realpath(x)
^
Python complains about the True: bit because it's expecting a statement like
(x if condition else y)
As jcomeau_ictx says, you should also leave out the == True when checking for booleans:
if x:
do_something
if not y:
do_something_else
you're missing a close parentheses on the previous line.