+ 1

Is there an easy way to find the datatype of a given variable entered by user?

Problem solving

5th Dec 2018, 1:23 PM
Himanshi Metta
Himanshi Metta - avatar
5 Respostas
5th Dec 2018, 1:43 PM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 3
In Python it is easy: input is always string, so if you want something else, you need to convert that string. It comes down to if the string even contains something convertible: if you try int('hello'), you will get an error. Like Muhd Khairul Amirin Bin Yaacob said, you can use exception handling: data = input() try: data = float(data) except: print('Enter a NUMBER I said!') So when float leads to an error, the except-branch will be executed.
5th Dec 2018, 1:46 PM
HonFu
HonFu - avatar
+ 1
You can use exception handling
5th Dec 2018, 1:25 PM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 1
But c doesn't have exception handlers
5th Dec 2018, 1:42 PM
Himanshi Metta
Himanshi Metta - avatar
+ 1
Yeah in C it's all messier... :) Just trying to get a handle of that myself right now.
5th Dec 2018, 1:49 PM
HonFu
HonFu - avatar