+ 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)

9th Apr 2022, 12:04 PM
AARAV PANDEY
AARAV PANDEY - avatar
5 odpowiedzi
+ 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 :-)
9th Apr 2022, 12:22 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 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.
9th Apr 2022, 12:39 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 2
Thanks 😊
11th Apr 2022, 11:40 AM
AARAV PANDEY
AARAV PANDEY - avatar
+ 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.
9th Apr 2022, 12:12 PM
Mustafa A
Mustafa A - avatar
+ 1
You are giving numbers So you have to convert into integer with help of int()
9th Apr 2022, 3:44 PM
UNKNOWN