python编辑elif显示错误_否则,elif,如果Python出现问题

1586010002-jmsa.png

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

解决方案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':

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值