[SOLVED]Can anyone tell me why this isnt working:x = input(3) y = input(16) print (x+y) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12

[SOLVED]Can anyone tell me why this isnt working:x = input(3) y = input(16) print (x+y)

27th Dec 2020, 12:27 AM
Albanianz
Albanianz - avatar
85 Answers
+ 13
Here's whats happening in the background. x = input("Enter input: ") is the same with x = input(15) - - - - - - - - - - - - - - - - - In this case the first one will be like this in the console. Enter number: The second one will be like this 15 - - - - - - - - - - - - - - - - - But you can still enter your input for both cases. For example I want to input my name " Nicko " Enter input: Nicko 15Nicko - - - - - - - - - - - - - As you can see here I can still input my desired input for both cases. The reason why it outputs 316 in your case is because here in SoloLearn, The input texts are displayed horizontally instead of vertically x = input(3) - - -> 3 is a text input y = input(16) - - -> 16 is a text input print(x + y) #And when you input nothing the SoloLearn console will be like this. 316 /*some errors*/ EOF to be specific - - - - - - - - - - - - - - - Try to input in sololearn like this: Hello World Then it will be 316 HelloWorld - - - - - - - - - - - -
27th Dec 2020, 1:52 AM
noteve
noteve - avatar
+ 5
If you write: x=int(input(3)) this number 3 does not effect the value of your variable x. It's only printed to the screen! So if you write... x=int(input(3)) y=int(input(9)) print(x+y) in a real ide it would print 3 to the screen then wait for your input, e.g. 5 -> this would look like 35 but x will be given the value 5 when you press <Return> then it jumps to the next line, prints 7 to the screen and waits for the next input, e.g. 3 -> <Return> -> next line. In this next line the sum of x=5 and y=3 is printed. In sololearn's playground you have to give all your input in one go. So it waits for two inputs, you submit the values and the code is executed, which means 3 is printed (no new line), 7 is printed (no new line)->now it looks like 37 (again no new line) -> now the sum of your submitted values is printed, e.g. 8... Now it looks like 378, but the result of adding 5 and 3 is 8! But what's your idea behind int(input(3)) ? What do you really want the code to do?
27th Dec 2020, 1:52 AM
Crash
Crash - avatar
+ 4
.Osama Deep. This could be explained in one comment like Царь СОБАКА - Догго I did. Dont confuse people like that lol.
27th Dec 2020, 1:39 AM
noteve
noteve - avatar
+ 4
Albanianz Here in SoloLearn if you input nothing in the input box, the variable that needs input wont have input so it will cause an error. What Crash did is to prevent that error by using default values with exception statements. If you input nothing, the code will cause an error so the except statement runs and the variable will have the value and there will be no error cause the variable now has the value. Although I dont think this is necessarry for you to know it right now as you will learn it eventually. Thanks and Keep Learning!!
27th Dec 2020, 1:36 PM
noteve
noteve - avatar
+ 3
Albanianz I still wonder what you wanted to do with the numbers in the brackets. Did you want to use default values ​​if the input is invalid? Then this could be something for you: ... https://code.sololearn.com/cF7E5m1CMH5V/?ref=app
27th Dec 2020, 1:23 PM
Crash
Crash - avatar
+ 3
Kode Krasher Thank you for sharing the info and the link. I'll definitely check this out!
27th Dec 2020, 10:31 PM
Crash
Crash - avatar
+ 2
the argument accepted by the input function is what the user will see as prompt on the terminal instead of a blinking cursor only. Also the input function ALWAYS returns the input as a string.
27th Dec 2020, 11:48 AM
John
John - avatar
+ 2
Crash I didn't want to do something specifik, I didn't understand what input is for and I saw a youtube video writing it like that so I was wondering why it says error
27th Dec 2020, 1:31 PM
Albanianz
Albanianz - avatar
+ 2
You must convert the string to a numerical value using int or float function. Hope it helps!!!
27th Dec 2020, 4:28 PM
Muiz
Muiz - avatar
+ 2
Aight, Thanks for those who helps! For those who will comment, It is already SOLVED so there is no need to answer. Thanks!
28th Dec 2020, 3:34 AM
noteve
noteve - avatar
+ 2
Aniket Kandara yes I already got the answer thank you
28th Dec 2020, 11:03 AM
Albanianz
Albanianz - avatar
+ 2
https://code.sololearn.com/cyI37ARakiu4/?ref=app check this, the idea isn't mine but did it with my memory and I think I'm getting this
28th Dec 2020, 12:44 PM
Albanianz
Albanianz - avatar
+ 1
Царь СОБАКА - Догго I idk can you explain that more? The values i know i shouldn't write them other people on my code should but I just took it as example
27th Dec 2020, 12:31 AM
Albanianz
Albanianz - avatar
+ 1
Царь СОБАКА - Догго I .Osama Deep. Nah its okay you're both helping me here
27th Dec 2020, 12:43 AM
Albanianz
Albanianz - avatar
+ 1
print (int(Input ())+int(input ())) For adding two integers
27th Dec 2020, 1:16 AM
Sâñtôsh
Sâñtôsh - avatar
+ 1
Crash 《 Nicko12 》 Thank you very much for taking the time to write that just to help a total stranger like me understand a simple thing like that, really helped me understand what I was doing wrong and that sololearn had a problem itself with that!
27th Dec 2020, 2:10 AM
Albanianz
Albanianz - avatar
+ 1
Try this X=int(input("Enter a Number:") Y=int(input("Enter second number:") Z=X+Y print("sum of X,Y is =",Z) Output: Enter a Number:3 Enter second number:16 sum of X,Y is= 19 👍👍
27th Dec 2020, 3:38 AM
Medi Siva Krishna
Medi Siva Krishna - avatar
+ 1
Inside the input() function you shoudnt put a number only string that appear to the user as a message telling him what he need to enter after that you can convert the input to integer using int() function and print the sum
27th Dec 2020, 3:26 PM
Badr Artib
Badr Artib - avatar
+ 1
I think it's great that everyone wants to help, but in the meantime so many identical or equivalent answers have been given that we should wait first and see whether the person asking the question still needs help ... I think he'll follow up if there are any questions left! Btw.... @the downvoter Maybe you should leave a constructive comment so that we know what you have to complain about ...
27th Dec 2020, 8:22 PM
Crash
Crash - avatar
+ 1
Int(input()) Input() by default returns a string, so you have to convert it to integer before any proper arithmetic operations can be performed
27th Dec 2020, 9:07 PM
Abdul Lee🎌🇳🇬
Abdul Lee🎌🇳🇬 - avatar