我想测试一个数字,看看它是否在一到三之间,但我认为我的解决方案很丑陋而且很基本。
有人可以提出更好的建议吗?
blah = ('woo','blah','foo','bah')
if int(tmpword[2]) >= 1 or int(tmpword[2]) >= len(blah):
return False, bpoints[int(tmpword[2])-1]
else:
return False, word
tmpword
是我从字符串中提取的一些数字。我只是想测试这个数字是否在 1 和 'blah' 的长度之间
最佳答案
if 1 <= int(tmpword[2]) <= len(blah):
等等
顺便说一句,您现有的代码也不能满足您的要求。你的意思可能是
if int(tmpword[2]) >= 1 and int(tmpword[2]) <= len(blah):
等等
关于python - 条件,测试某个范围内的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8380996/