+ 2

string in the "simple calculator" lesson

why do we need the "str" in this code: elif user_input == "add": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 + num2) print("The answer is " + result) im confused........

19th Mar 2018, 8:11 AM
CodingIsFun
CodingIsFun - avatar
3 Respuestas
+ 1
print("The answer is"+result ) in this line "The answer is" is a string and output is integer we cannot add String and integer So we have convert integer output in String to display with String...
19th Mar 2018, 8:14 AM
Dinesh Banjara
Dinesh Banjara - avatar
+ 8
so that it doesn't try to use math on a string and error out... notice the plus sign in the print statement!
19th Mar 2018, 8:14 AM
Ahri Fox
Ahri Fox - avatar
0
thx ahri and dinesh! :)
19th Mar 2018, 8:21 AM
CodingIsFun
CodingIsFun - avatar