0

Str(num1 + num 2)?

If we don't use str command and add two numbers as float, can we show that with print("result is: ", num1+num2)?

2nd Jun 2019, 4:44 PM
Mohammad Akradi
Mohammad Akradi - avatar
2 ответов
+ 5
Hope i understand what you mean: num1 = 4.5 num2 = 7.2 print('result is: ', num1 + num2) #output is: result is: 11.7 if you get the values for num1 and num2 from input() function numbers has to be converted to int or float. The reason is that input() always returns a string like ‘4.5’ and ‘7.2’. Addition can not be done with strings, the strings will be concatenated: num1 = input(‘num1’) num2 = input(‘num2’) #num1 shows ‘4.5’ #num2 shows ‘7.2’ print('result is: ', num1 + ‘ ‘+ num2) #output is: result is: “4.2 7.2” to get int or float change input() to: num1 = float(input(‘num1’))
2nd Jun 2019, 4:52 PM
Lothar
Lothar - avatar
+ 1
Yes , str() constructs a string from a wide variety of data types, including strings, integer literals and float literals
2nd Jun 2019, 4:55 PM
**🇦🇪|🇦🇪**
**🇦🇪|🇦🇪** - avatar