You are right: the issue is with how you use the any()
function. any()
returns either True
or False
depending on whether or not any of the elements in its iterable argument are True
, in other words if they exist and are not False
or an empty list or empty string or 0. This will always evaluate as True
for int
because int
does contain an element that is True
and the same will happen for d
unless you do not give any input when the input()
function prompts you (you hit return without typing anything). What your conditional is basically asking is the following:
if True==True
To fix this, just change your code to the following:
a = 0
int = ["1","2","3","4","5","6","7","8","9","0"]
while a == 0:
print("Please choose any word by typing it here:")
d = input()
print(d)
for letter in d:
if letter in int:
print("Invalid input. Please choose a non-number with no spaces or special characters.")
break
The easiest solution, however, does not involve the int
list at all. There is actually a special method in python that can gauge whether or not a string has a number (or special character) in it, and this method, .isalpha()
can be used to streamline this process even further. Here's how to implement this solution:
while True:
d = input("Please choose any word by typing it here: \n")
print(d)
if not d.isalpha():
print("Invalid input. Please choose a non-number with no spaces or special characters.")