I'm a beginner in Python, trying to make a simple program combining everything I've learnt so far. In this question the program itself is not the question, but a specific part of it, which I have worked around, but wanted some insight into why this is happening.
def printresults (answer):
if answer == "y" or "Y":
print ("Yes")
else:
print ("No")
In the case above, I have managed to get this function to run with "N" as the 'answer'. The issue I faced was that it would then run the first if-branch, and in this case print "Yes", despite it not being a "y" or a "Y".
I have since worked around this issue by creating a new variable in the function and using the lower() method to make only one possibility, and then simply asking it to determine if it is "y" or not.
def printresults (answer):
lwranswer = answer.lower()
if lwranswer == "y":
print ("Yes")
else:
print ("No")
I therefore suspect the fault lies with me somehow misusing the "or" operator - I just don't know how or why. Any insight would be much appreciated.
Thanks
Note, the program won't simply return a Yes or No, but I made it print these to troubleshoot and see which branch it was taking.