Ask coding questions

← Back to all posts
I can't work out why adding these two ints just gives zero
Juice99 (6)

This has been fixed, I originally had: x = int();y = int()
but I needed: x = int(x); y = int(y)

I am new to programming and am testing out functions so I made this tiny program that is supposed to add two user input numbers but it just gives out zero,

I know that the numbers are going into the function, They're just not being added and I have no idea why.

Any clues would be greatly appreciated.

Answered by Vandesm14 (2383) [earned 5 cycles]
View Answer
Vandesm14 (2383)

You are printing x+y, not returning x+y, which is what you want.
So you want: return x + y

Juice99 (6)

@Vandesm14 Thank you for the answer.

This doesn't seem to display anything other than the two input numbers, and I'm still not sure why what I had doesn't work.

Vandesm14 (2383)

@Juice99 Because you're running a function, which usually returns something. You'd want to print the value of the function like so print(add(1,2)) and make sure that the function is returning the addition return x + y

Juice99 (6)

@Vandesm14 Sorry if I'm being slow but I've put print(add(w,z)) and it's still displaying zero

Vandesm14 (2383)

@Juice99 I found the issue. You need to add x and y into the int(), like this

x = int(x)
y = int(y)
Juice99 (6)

@Vandesm14 ah yes, Thank you. It works now, strangely enough in one of my other Repls I had it like that, not sure why I didn't remember that.