I am a new learner in programming, and I was trying to check whether a word exists in a file. I put the words into a dictionary (the exercise told me to put it in a dict as a key and ignore the value), and then let the user input a word, then I tried to check whether it is in the dictionary.
words_dict = dict()
fhand = open('desktop/codingMaterial/words.txt')
for line in fhand:
line = line.rstrip()
t = line.split()
for word in t:
words_dict[word] = 0
# check if word exists
v = input('Enter a variable: ')
if v in words_dict == True:
print('Exist.')
else:
print('Does not exist.')
I tried to run this, but no matter which word I input, the output was always 'Does not exist.'. I think I did something wrong in the if statement, but I am not sure how to fix it.
Aucun commentaire:
Enregistrer un commentaire