0

If i put user == 'Q' only in the if block !! then it loops and works but if i write user == 'Q' or 'q' then it fails why??

#take.txt already exist in the same folder !! with open('take.txt', 'a') as ask: a = True while a: user = input('Why do you like programming?\n(Type Q to quit)\n') if user == 'Q' or 'q': break ask.write(user)

19th May 2020, 2:44 PM
Lucky Nayak
Lucky Nayak - avatar
4 Réponses
+ 2
If user == "Q" or user =="q": Everybody comes across that bug on his coding journey 😉
19th May 2020, 2:48 PM
Oma Falk
Oma Falk - avatar
+ 1
I "q" evaluates True because "q" is a not empty string. If user == "Q" or "q": Is therefore always true. Some better opportunities might be If user.lowercase() =="q"# oops was it lowercase? Or If user in["Q","q"]: Just as u want
19th May 2020, 3:08 PM
Oma Falk
Oma Falk - avatar
0
Thank you oma falk!! but why not user == 'Q' or 'q'?? please explain
19th May 2020, 2:49 PM
Lucky Nayak
Lucky Nayak - avatar
0
I didn't understand but its ok!!
19th May 2020, 5:27 PM
Lucky Nayak
Lucky Nayak - avatar