- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在网上查看了如何在 python 中编写 if in range 语句,我很困惑为什么我的 print 语句没有出现。
print("Hello User What Is The Wind Chill Today?")
windChill = int(input())
if windChill <= -25:
print("No School!!!")
if windChill >= 0:
print("Theres School ):")
if int(windChill) in range(-1, -24):
print("Maybe school")
预期的结果是,如果我输入 -20,它会说“可能是学校”,但现在当我输入从 -1 到 -24 的数字时,它不会显示打印语句。
最佳答案
Python 的 range函数的值递增 1。range
中的第一个参数是start
第二个是 stop
第三个(可选)参数是step
这就是它的增量。您在这里有 2 个选项。
要么从较低的数字开始到较高的数字停止:
if int(windChill) in range(-24, -1):
或者保留你所拥有的但告诉range
增加-1:
if int(windChill) in range(-1, -24, -1):
以上两者都会为您实现相同的结果,尽管仅供引用,如果您运行 list()
,它们都会导致彼此的反向列表。在他们身上
print(list(range(-24, -1)))
#[-24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2]
print(list(range(-1, -24, -1)))
#[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20, -21, -22, -23]
编辑
如果深入挖掘,范围是使用以下公式确定的:
For a positive step, the contents of a range
r
are determined by the formular[i] = start + step*i
wherei >= 0
andr[i] < stop
.For a negative step, the contents of the range are still determined by the formula
r[i] = start + step*i
, but the constraints arei >= 0
andr[i] > stop
.
所以本质上,range
实际上在我的示例中不包括停止值(分别为-1
和-24
) 所以一定要考虑到这一点。使用
if int(windChill) in range(-24, 0):
#or
if int(windChill) in range(-1, -25, -1):
关于Python 打印语句未显示在 If in range 语句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54544093/
我是一名优秀的程序员,十分优秀!