"not in" identity operator not working when checking empty string for certain characters
An empty string is present in any string. Therefore your condition, difficulty not in 'EMH'
will evaluate to False
when difficulty
equals ''
; so the while loop's body won't be executed.
In [24]: '' not in 'EMH'
Out[24]: False
In [33]: '' in 'EMH'
Out[33]: True
A better approach might be to convert the string EMH
to a list via list('EMH')
so that something like EM
or EH
, or a empty character doesn't break your loop, or avoid it from starting in the first place
Also as @Blckknght suggested, a better alternative is use a default value of None
for difficulty.
In [3]: difficulty = None
In [4]: while difficulty not in list('EMH'):
...: print('Enter difficulty: E - Easy, M - Medium, H - Hard')
...: difficulty = input().upper()
...:
Enter difficulty: E - Easy, M - Medium, H - Hard
A
Enter difficulty: E - Easy, M - Medium, H - Hard
B
Enter difficulty: E - Easy, M - Medium, H - Hard
C
Enter difficulty: E - Easy, M - Medium, H - Hard
EM
Enter difficulty: E - Easy, M - Medium, H - Hard
E
In [5]:
I think what you need to use is a list, instead of a string:
difficulty = ''
while difficulty not in ['E','M','H']:
print('Enter difficulty: E - Easy, M - Medium, H - Hard')
difficulty = input().upper()
This is a good case for a do-while loop condition. But, python doesn't have it. Please check if the below style suits you:
while True:
print('Enter difficulty: E - Easy, M - Medium, H - Hard')
difficulty = input().upper()
if difficulty not in 'EMH': #or in ['E', 'M', 'H']
continue
else:
#do some logic
break
I do like @Emmet B's suggestion to use ['E', 'M', 'H'] in a loop rather than as a string. It makes sense also as you are expecting one of the character as input and not set of characters.