I think you have good concept, I see the code and is logical what you are trying to do. What you are mistaken is How logic operators works in Python. Anyway I see three main errors in your code, one by which leads you to believe that the issue is to check the string data type (but is not that the problem.
The first and second Issue made you believe that the programm could not check whether the user input is a string or not ("For determining int value is okay but string value is not working.")
First Issue
Truthy, Falsy values
response == int or float
This will always be True
, because of the or float
.
Because even if the First check if False, or float
is checking if the float type is truthy and it is always truthy!.
print(bool(float))
# >>> True
if float:
print("Floating....")
else:
print('....')
# >>> Floating....
# Infinite Loops, these are all equal
while float:
....
white bool(float):
...
white True:
....
Second Issue
Understanding how Conditional statements and or
works.
Again, is here:
response == int or float
You should use:
while response == int or response == float:
In Python (and most languages I believe) you can't concatenate conditional operators like in English, that line could be more clearly written as so:
(response == int) or (float) # 1 version
(response == int) or (bool(float) == True) # Converting float to
So logically, I can see that you wanna say:
'Check if the value is a integer or a float'
But how is written in your code is saying
'Check if the value is a Integer but if is not then check if float is a True value'*
Third Issue
Data type from ìnput is always a string!
The function then reads a line from input, converts it to a string
I think you did not notice of this one, because the previous 2 errors made you think that the program could not check when the value is a string.
Now you see that the problem is actually to check if the value is a int or float, not a string.
Solution
This would be my solution, instead of do too many nested conditional checks, you better of do it in a try except blocking in a “Ask forgiveness not permission” - explain style.
# Input Checks
response = input("\nWhere raw data located(folder)? : ")
print(response)
print(type(response)) # Check the type, you will see is always a string
while True:
try:
float(response) # Not need to check for a int, float checks for both (and int too it will truncate the float)
except ValueError:
print("String Found!!!")
break
else:
print('Float or int found, programs keeps running..')
pass
About truthy values
Source --> What is Truthy and Falsy? How is it different from True and False?
All values are considered "truthy" except for the following, which are "falsy":
None
False
0
0.0
0j
decimal.Decimal(0)
fraction.Fraction(0, 1)
[]
- an empty list
{}
- an empty dict
()
- an empty tuple
''
- an empty str
b''
- an empty bytes
set()
- an empty set
- an empty
range
, like range(0)
- objects for which
obj.__bool__()
returns False
obj.__len__()
returns 0
A "truthy" value will satisfy the check performed by if
or while
statements. We use "truthy" and "falsy" to differentiate from the bool
values True
and False
.
[Truth Value Testing](https://docs.python.org/3/library/stdtypes.html#truth-value-t