I'm trying to compare 2 elements : one is a user input and the other is an element from a list. The fact is that when the user enter a value that is in the list, the program print "Ok". But since there's two type of element in my list (integer and string), the user can only compare his value with every string of the list...
list = ["test", "testOne", 5, 10]
continuer = True
while continuer:
word = input("Enter a word : ")
for elements in list:
if word in list:
print("Ok")
break
else:
print("Not ok")
break
I really need these numbers to be integers, can you give me some advices ? Thanks !
6 Answers 6
You can add something like this:
word = input("Enter a word : ")
try:
word = int(word)
except ValueError:
print("Values is not int")
Comments
- Avoid using
listas a variable name - Use string data in your list
- Use
into check element presence in your list
Code:
lst = ['test', 'testOne', '5', '10']
word = input('Enter a word : ')
if word in lst:
print('OK')
else:
print('Not OK')
Comments
To check user input value for each of the elements from a list containing both String and Integer type data, we can use a try-except block.
- At first, we will check if the user input and list item contain integer data in a
tryblock. If the data matches, we will printOkto the user. - If the above
tryblock throws an exception, we will verify if both values are matched. If the data matches, we will printOkto the user.
If none of the above cases are affirmative for all the items on the list, we will print Not ok to the user.
data = ["test", "testOne", 5, 10]
while True:
word = input("Enter a word : ")
if word == "exit":
break
word_found = False
for element in data:
try:
if int(word) == int(element):
word_found = True
break
except ValueError:
if word == element:
word_found = True
break
if word_found:
print("Ok")
else:
print("Not ok")
Output:
Enter a word : Shovon
Not ok
Enter a word : test
Ok
Enter a word : 34
Not ok
Enter a word : 5
Ok
Enter a word : Ahmedur
Not ok
Enter a word : testOne
Ok
Enter a word : exit
Comments
- You don't need to iterate through the list using for loop as you can check existence of an element using "in" operator So
if word in list:
print("OK")
is sufficient.
- If you want to ensure only integer is entered, just validate the user input like int(word)
Comments
Thanks for your answer guys ! I tried it in another way
list = ["test", "testOne", 5, 10]
continuer = True
def safe_cast(val, to_type, default=None):
try:
return to_type(val)
except (ValueError, TypeError):
return default
while continuer:
word = input("Enter a word : ")
for element in list:
if isinstance(element, str) and element == word:
print("Ok")
break
elif isinstance(element, int) and safe_cast(word, int, 0) == element:
print("ok")
break
And it worked well ! Sorry for the inconvenience
Comments
You can try this
list = ["test", "testOne", 5, 10]
continuer = True
getlist1=str(list) #Converting all the elements in list to string
while continuer:
word = input("Enter a word : ")
for elements in list:
if ((word) in (getlist1)):
print("Ok")
break
else:
print("Not ok")
break
Output: Enter a word : 5
Ok
Enter a word : 10
Ok
Enter a word : test
Ok
Enter a word : testOne
Ok
Enter a word : hello
Not ok