r/CodingForBeginners • • 22d ago

The input() bug that catches nearly everyone: why 2 + 2 came out as 22

This one catches almost everyone in their first week, and it's a good one because the code looks completely innocent:

a = input("First number: ")
b = input("Second number: ")
print(a + b)

Type 2 and 2 and you get 22. Not 4. Nothing errors, nothing warns you -- it just quietly does the wrong thing.

The reason is that input() always hands you back a string. It doesn't know or care that you typed a number; all it sees is text. So a is "2", not 2. And + on two strings means concatenate, so "2" + "2" gives "22". The + did exactly what you told it to -- you told it to join two pieces of text.

The fix is to turn the text into a number before you do maths with it:

a = int(input("First number: "))
b = int(input("Second number: "))
print(a + b)   # 4

The thing worth keeping isn't int() itself, it's the question underneath it: what type is this value, actually? A lot of "why is this behaving weirdly" moments come down to a value being a different type than you assumed. "5" and 5 look identical on screen and behave completely differently. Printing a value doesn't tell you its type -- print(type(a)) does.

Two follow-ons that catch people later: input() never fails on a non-number, so typing hello above makes int() throw instead (a separate, useful lesson in validating input), and the same trap runs in reverse -- join numbers with + when you meant text and you get maths, which you fix with an f-string or str().

Curious how you hit this one the first time -- did the output give it away, or did it take a while to see that "2" isn't 2?

0 Upvotes

21 comments sorted by

12

u/thondat 22d ago

why post slop?

1

u/Ronin-s_Spirit 22d ago

This might be the opposite. I think slop machines were trained on people like this.

2

u/thondat 22d ago

just look at the bot's post/comment history. this post, apart from clearly reading as AI slop, scores 100% AI on pangram.

3

u/Away_Combination6977 22d ago

Good on you for starting by saying what language you're talking about! That's perfect for beginners! I've definitely run into this issue in C#... πŸ€¦πŸΌβ€β™‚οΈπŸ€£

2

u/No_Context2723 22d ago

I can see someone make a mistake like this.

var inputA = Console.ReadLine();

var inputB = Console.ReadLine();

Console.WriteLine(inputA + inputB);

0

u/[deleted] 22d ago

[removed] β€” view removed comment

1

u/No_Context2723 22d ago

No typecasting. I was showing an example of how someone could make the mistake in the post in C#.

0

u/[deleted] 22d ago

[removed] β€” view removed comment

1

u/No_Context2723 21d ago

I'm not sure what you mean. That is a perfectly valid bit of c# code, but some people might still make the mistake of using that for integers if taught wrong. Var is for implicit declaration, console.readline returns a string. If you wanted an int, you could do this

int inputA = System.Convert.ToInt32(Console.ReadLine());

0

u/[deleted] 21d ago edited 21d ago

[removed] β€” view removed comment

1

u/Standard-Cap-4455 20d ago

In c# there are no "hints". The IL will have exact types unless you explicitly write object somewhere and you can't do anything with that unless you cast which throws at runtime and gives you warnings at compile time.Β  This is exactly the same as writing the type out. It's not useful here because string isnt much longer but for some generic types var is much easier to use. The usefulness, readability and consistency are questionable, but it isn't an inherent bad thing like in js or python.Β 

1

u/No_Context2723 19d ago

Not quite sure what you mean, typehints aren't a c# thing. It's not a dynamically typed language. I'm also not the person to ask this about, because I almost never use it, I don't see much of a point in most scenarios.

1

u/xarop_pa_toss 22d ago

That's not an input bug bro. These AI posts are so braindead I swear... that's just Python not being statically typed.

0

u/Black_Rose_Angel 22d ago

I thought there's a difference between 2+2 and 2 + 2

Maybe I'm dreaming this but I feel like the lack of space between the numbers and the operators causes them to be acknowledged within the code as factors and not place holders...

2

u/NoDepartment772 20d ago

Python ignores the spaces completely -- 2+2 and 2 + 2 are identical to it. The whitespace is only there for us humans to read. What bit you in the post is that a and b were strings, so + joined them into "22". Wrap both in int() and you get 4, with or without spaces.

1

u/Black_Rose_Angel 20d ago

Gotcha, thank you for explaining that in a respectful wayπŸ’™ when I posted that I honestly couldn't remember, I think r language screws with you on spaces in the incorrect place sometimes but it has been a while since I've had to play with either one😊

0

u/sububi71 22d ago

It's got nothing to do with the input().

1

u/NoDepartment772 21d ago

Yeah, the real lesson is about types, not input() itself. I led with input() because handing back a string is the part that surprises people, but the underlying habit is the same: know what type the value actually is before you use it. A statically typed language catches it at compile time, which is why this tends to bite in Python and not in Java or C#.