I am new at python. Here is my wiered python code Which works fine for all inputs except
when c=0 and r!=0 .
I have number of test case(tc) , r and c as inputs which give required output depending condition.
Question—For input r=4 & c=0 ,Output Should be 2,but output is coming 1 . I am geting wrong answer for every r!=0 & c=0.
Code:
tc=int(input())
while tc:
r,c=raw_input().split()
if int(r)%2==0 and r!=2 and r!=0 and c!=0:
r=int(r)/2
elif r!=2 and r!=0 and c!=0:
r=int(r)/2+1
elif r==0 or r ==2:
r=1
if r!=0:
if int(c)!=0:
print(int(r)*int(c))
else :
if int(r)%2==0 :
print(int(r)/2)
else:
r=int(r)/2+1
print(r)
else :
print(c);
tc=tc-1
sample input and output
4 //tc
10 10 //r=10 c= 10
50 //fine
3 3 //r=3 c=3
6 //fine
4 0 //r=4 c=0
1 //Should be 2 accoring to code
5 0 //r=5 c=0
2 //Output should be 3 accoring to the code
You have solved the mystery yourself (kind of):
ris a string. Sorwill always be!=2because"2" != 2is always true. Same goes for all the other comparisons.I’m guessing you first got a
TypeErrorwithif r%2==0, so you changed that bit of the program (also in all the other places where you’re actually doing calculations with the values) but neglected to apply this insight to the other parts of the program.So first convert all your inputs to
ints, then start applying your program logic.