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.
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
(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