1

Here is my explanation. This code is what the user inputs the 'string' value.

And for the while statement in this code, I want to make a code like "if the input value is not string value, then print the messages until the program gets string value ".

I tried many things while not response == str:, while not response == str(response):, while response == int or float:, while response == int and float:, while response == int or response == float, etc. All these things didn't work for my purpose.

Some of them allowed all types of values and didn't do the loop. Others just showed loop again and again even the value is a string.

I also made other code for determining int value(Showing an error message if the input is not int) and it works well. But for the 'string' value, it doesn't work.

Is my code to determining string value with while statement wrong?
For determining int value is okay but string value is not working.

I am confused since I think I didn't do any mistakes.
Can someone help me out?

    response = input("\nplease enter the value : ")

    while response == int or float:                                                                                            

        print(" Enter the value ")
        
   
        response = input("\nplease enter the value : ")
Federico Baù
  • 6,013
  • 5
  • 30
  • 38
Bella Lee
  • 97
  • 1
  • 7
  • You are comparing ```response``` to data type. ```response == int or float``` is never True. You should do ```while type(response) not in [int, float]```. ```input``` returns a string. And a ```string``` data type is not in the list of the data types –  Aug 20 '21 at 16:08
  • @Sujay `réponse == int or float` is *always* True, since `float` is a truthy value. – chepner Aug 20 '21 at 16:22

2 Answers2

2

You cannot compare a string to the data type. It will always be False. Also, int or float the latter part will evaluate to True.

You must use type to check the data type of the argument

response = input("\nWhere raw data located(folder)? : ")
while type(response) not in [int, float]:
  • Thank you for your comment. I got your point which is good. But when I tried this code, the loop is never ended. I don't understand why this is happening.... – Bella Lee Aug 20 '21 at 16:54
  • Because the next time, you are assigning the input to ```welcome_input```. Instead, re assign it to ```response```. @BellaLee –  Aug 20 '21 at 16:55
  • Thank you and I just found that the system thinks 1 as a string. So I think the system allows every value even it is a number. Is this normal? Is there any good way to solve this? Thank you a lot – Bella Lee Aug 20 '21 at 17:11
1

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

Federico Baù
  • 6,013
  • 5
  • 30
  • 38