r/CodingHelp • u/Xxhardman69xX • Jun 11 '19
Help pls why does this equal 34 apparently aren’t you converting them to integers so it should be 7
7
u/tcaimmi Jun 11 '19
Because they are as text, not as numbers. It is like putting a number 3 next to a numer 4,not adding them. For example if I write ("I" +"like that") the text would be I like that. Let me know if you understood
8
1
3
u/PC__LOAD__LETTER Jun 11 '19
Order of operations. You’re adding “3” and “4” first, which are strings. When you “add” strings with the + operator, they’re concatenated. “34”. Then “34” is being converted to an int: 34.
5
u/spaghettu Professional Coder Jun 11 '19
This is why some programming languages (ie. PHP) don't use the "+" operator for strings, because in this context you are not performing an addition. This operation is called concatenation. In many languages "+" is used to mean both, when the two operations mean completely different things. So this is a great introductory question because it makes sure you understand the difference.
1
u/SupremeRedditBot Professional Bot || Mod Jun 11 '19
Please Add A Flair To Your Post!
Suggested Flair: [Random] or [Meta]
To add a flair:
Click
flairunderneath your postSelect a flair
Click
save
I am a bot run by /u/SupremeDesigner for /r/CodingHelp || This was an automated ^response
1
u/MrQuickLine CSSophile Jun 11 '19
You'd get 7 if you did int("3") + int("4"). The function evaluates what's in the brackets first. What's in the brackets is two strings concatenated. If you evaluated "3" + "blue" you'd get "3blue" (and an int on that would return NaN). You're evaluating "3" + "4" which is two strings, giving you "34" (as a string). Then the int runs on what's inside the brackets. So it's giving you int("34") which returns 34.
1
u/skulltey Jun 11 '19
So in layman's terms, int("34") essentially coerced the "34" string into the int value of 34? Kinda like when you do something like: ("34" == 34) or how putting a "!" Outside a True condition makes it false? (Mostly familiar with JavaScricpt and a bit of Java).
1
1
Jun 11 '19
are you not adding "3" + "4" which are strings, so "34" then converting it into integer int(34) so... it just 34.
1
u/reallybadpennystocks Jun 12 '19
Does it annoy you that all of the learn to code classes on sololearn are mostly math lessons? Bc it does me
1
u/Slim_Shady001 Jun 12 '19
If you doing >>>int "3"+"4"="7"
Or If you doing >>>int"3"and "4"= 34
To Know How Is It done, Click Here.
38
u/SnowyDark Professional Coder Jun 11 '19
You're converting the result of "3" + "4" to an int, meaning that the addition occurs before the number conversion. As a result, the strings "3" and "4" combine into "34", which is then converted into the integer value 34.