1
instrument=(raw_input("Which kind of tab would you like to view? Enter 'Guitar' or 'Bass' for a random tab " ))
print
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

0

2 Answers 2

8
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':
Sign up to request clarification or add additional context in comments.

1 Comment

I think you mean ('guitar', 'Guitar').
7
instrument == 'guitar' or 'Guitar'

should be

instrument == 'guitar' or instrument == 'Guitar'

or better,

instrument in ('guitar', 'Guitar')

or even

instrument.lower() == 'guitar'

and the same for the bass portion.


The reason why it's always outputting the first option is that a == b or c...

...is actually read as (a == b) or c.

The or operator is not used how you think it is. It is a logical or, not a "multiple options" separator.

1 Comment

That 'bible' variable actually should've been 'instrument', sorry I fixed it in my question. But your examples still stand true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.