instrument=(raw_input("Which kind of tab would you like to view? Enter 'Guitar' or 'Bass' for a random tab " ))
if instrument=='guitar' or 'Guitar':
print ("0-3-5---0-3-6-5---0-3-5-3-0")
elif instrument=='bass' or 'Bass':
print ("3-5-12--12-0-5-6-0-0-0-3")
else:
print 'Sorry, please re-enter a proper answer'
This code prints out the 'guitar' tab(the first if statement) every time, no matter what you type in for the variable 'instrument'.
I'm trying to teach myself python and am just jumping in and learning as I go, this is a basic program I'm working on and want to expand on.
I've looked online but from what I've read about if else statements, it looks to my novice eyes that this code should work. I'm missing something that I just can't figure out
解决方案if instrument=='guitar' or 'Guitar':
is parsed like
if (instrument=='guitar') or ('Guitar'):
Non-empty strings like 'Guitar' are evaluated as True, so the first condition is always True.
Instead, use
if instrument in ('guitar', 'Guitar'):
or, if you are willing to accept funny spellings like 'gUiTAR', you could use
if instrument.lower() == 'guitar':