Conditional ("n" or"N") not working in program | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Conditional ("n" or"N") not working in program

Hi, The below code is a dictionary search code, what's not running is that, when the condition checks "j = input("did you mean %s?"% gm(w, data.keys())[0])", and I press n or N, the program just exits out, it doesn't prints the message in that condition. Any help would be appreciated. import json from difflib import get_close_matches as gm data= json.load(open("data.json")) def search(): w = input("enter the word you want the meaning for:") w = w.lower() if w in data: return data[w] elif w.title() in data: return data[w.title()] elif w.upper() in data: return data[w.upper()] elif len(gm(w, data.keys()))>0: j = input("did you mean %s?"% gm(w, data.keys())[0]) if j == ("y" or "Y"): return data[gm(w,data.keys())[0]] elif j == ("n" or"N"): return "the word does not exists" else: return "invalid entry" else: return "the word doesnt exist" op = search() if type(op)==list: for i,j in enumerate(op,1): print(i,j)

10th Jul 2018, 2:13 PM
Aditya Rathore
Aditya Rathore - avatar
1 Answer
+ 2
use "j in ('y','Y')" or "j == 'y' or j == 'Y'"
10th Jul 2018, 3:12 PM
Mert Yazıcı
Mert Yazıcı - avatar