Nov-09-2021, 04:21 PM
allowable_letters = 'abcdefg' while True : test_string = input ('\nEnter a letter or number :') if test_string in allowable_letters : print (f' "{test_string}" IS in the allowable letters.') else : print (f' "{test_string}" is NOT in the allowable letters.')In the above code, entering a letter or number or even a string like ‘abc’ or ‘BashBedlam’ has the correct and expected result. However, if you press enter without typing anything else in, it will tell you that what your input IS in the allowable letters.
Output: "" IS in the allowable letters.
If you add print (ord (test_string))
in between the fourth and fifth line, the error will say :Output:TypeError: ord() expected a character, but string of length 0 found
So… my question is, why does python think that nothing or a zero length string IS in ‘abcdefg’?