problems with python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

problems with python

Python is such a strange language for a learner like me who is used to C/Cpp.How to deal with the datatype of a variable?For example,if input x=5,y=6,print(x+y),this will output "56".These problems will never happen on C/Cpp.

6th Feb 2021, 2:08 AM
Elvish
Elvish - avatar
2 Answers
+ 2
if input x == '5' ans y == '6', then x+y == '56'... because input() always return a string (str) type. to get number, you need to use int() or float() to convert it as number... then if x == 5 and x == 6, then x+y == 11. but if x and y are not of 'compatible' type (ie x == 5 and y == '6'), you will get an error ;)
6th Feb 2021, 2:15 AM
visph
visph - avatar
0
Code goes.Thanks a lot.
6th Feb 2021, 2:27 AM
Elvish
Elvish - avatar