Python Forum
Getting false even when data exists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting false even when data exists
#11
You ask for an integer in the prompt, but "input" returns a string. It doesn't matter what the user types. If they type "1" or "one" the return type is a string.
while True:
    number = input('Enter number ')
    if not number:
        break
    print(number, type(number), number==1)
Regardless of what you type, a string is never equal to an integer.
Output:
C:\Users\...>python junk.py Enter number one one <class 'str'> False Enter number 1 1 <class 'str'> False Enter number
When I type 1, this is input as the string '1'. The string '1' is not equal to the int 1. If your dataframe contains numbers for Customer Key, you will have to convert the input from a string to an integer (use int(str)). But you should first verify that Customer Key really contains ints and not strings.
Reply
#12
Let's assume that we have csv file with following data:

Output:
Customer_ID, Customer_name 42, Meaning of Life 1967, The Mother of All Demos 65, Benevolent dictator for life
What we gonna do:

- read csv file into dataframe
- create function to validate user input to be integer
- check wheter user input is in Customer_ID column

Importing csv into pandas:

>>> df = pd.read_csv('data.csv')
>>> df
  Customer_ID                  Customer_name
0           42               Meaning of Life
1         1967       The Mother of All Demos
2           65  Benevolent dictator for life
>>> df['Customer_ID'].dtype
dtype('int64')
We have data in dataframe and Customer_ID column is integer.

Now we write function for user input validation, which will ask an answer until it is convertible to integer:

def validate_user_input(message):
    while True:
        try:
            answer = input(message) 
            return int(answer)
        except ValueError:
            print(f'Expected integer but answer was {answer!r}')
Now we check 3 times whether user entered integer is in Customer_ID. While in pandas do as pandas so I am using pandas .isin and .any:

for i in range(3):
    from_user = validate_user_input('Enter Customer_ID as an integer: ')
    print(f'{from_user} is in Customer_ID: {df["Customer_ID"].isin([from_user]).any()}')
With some arbitrary answers it looks like:

Output:
Enter Customer_ID as an integer: e Expected integer but answer was 'e' Enter Customer_ID as an integer: 1 1 is in Customer_ID: False Enter Customer_ID as an integer: 42 42 is in Customer_ID: True Enter Customer_ID as an integer: 65 65 is in Customer_ID: True
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#13
(May-18-2021, 05:59 PM)ibreeden Wrote: But have you tried what Perfringo suggested? In what you show us you still don't convert the user input to integer.
(May-18-2021, 01:44 PM)sriniyum Wrote: CustomerKey = input("enter the Customer Key: ")

(May-18-2021, 05:59 PM)ibreeden Wrote: But have you tried what Perfringo suggested? In what you show us you still don't convert the user input to integer.
(May-18-2021, 01:44 PM)sriniyum Wrote: CustomerKey = input("enter the Customer Key: ")

What Perfringo suggested is not working. converting to integer has no use. Please loc below. even after converting to int and then validation is failing (the customer is key is valid)

CustomerKey = int (input("enter the Customer Key: "))
print (CustomerKey)
if CustomerKey in DataFrame:
print('True')
else:
print('False')

Output: enter the Customer Key: 51108
51108
False
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  p]Why os.path.exists("abc/d") and os.path.exists("abc/D") treat same rajeev1729 1 532 May-27-2020, 08:34 AM
Last Post: DeaD_EyE
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 852 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  How to insert data if not exists in mysql? farah97 0 1,174 Dec-29-2019, 08:32 AM
Last Post: farah97

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020