python - 条件,测试某个范围内的数字

标签 python conditional-statements

我想测试一个数字,看看它是否在一到三之间,但我认为我的解决方案很丑陋而且很基本。

有人可以提出更好的建议吗?

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/

相关文章:

python - Django 从 id 中过滤多对多

python - 为什么 Twisted 的 DeferredFilesystemLocks 对于并发使用不安全?

python - 计算合并字符串中的元音数量

javascript - JS 条件语句不起作用

Javascript if 条件与子字符串

mysql - MySQL 中需要进行连接的条件列

python - Python 中的贪心算法

python - 空格分隔的 csv,列名和值中有空格

r - 当使用 R 满足某些条件时,使用两列创建一个新变量

Mysql 与 Between\in 范围条件