+ 2
Why doesn't this code work after giving the inputs?
ans=(input(1st number to be added)) ans2=(input(2nd number to be added)) print(ans+ans2)
6 Answers
+ 5
Hello :-)
1. you also need the actual strings inside input():
input("1st number to be added:")
2. When you do, you'll be printing a concatenation of strings ans and ans2.
say 1st input is 6 and 2nd is 23
623 will be printed.
3. If you want to add the actual numbers represented by the strings, you do what Mustafa A says:
ans = int(input(" Bla bla"))
This time, 29 will be printed.
edit:
4. if your numbers are floats, then ans = float(input("bla bla"))
All of this stuff is in the courses though :-)
+ 2
Oh, btw: you do not write the input itself inside input() .
You write the input itself when you run your code, and are prompted to write it, if thatâs what you've been asking.
What you write inside input() is just a description of what is to be entered as input.
The actual input is entered when you run the code, because, you want to be able to assign different values to your ans variable object. you want to be able to use the same code for different values so you want your code to ask the user what value to enter "this" time.
Also:
the descriptions inside an input are always in quotes(strings)
and
the input you enter is also taken as a string which is why you have to explicitly pass the input inside an int() or float() func.
+ 2
Thanks đ
+ 1
Input takes strings. You will have to convert them to int in order to add the numbers.
put the "input" calls inside a "int" call.
+ 1
You are giving numbers
So you have to convert into integer with help of int()