Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中计算范围之间的数字_Python_Python 2.7 - Fatal编程技术网

在python中计算范围之间的数字

在python中计算范围之间的数字,python,python-2.7,Python,Python 2.7,a=5,b=8 我需要在for循环中使用这些数字 for i in range(a, b): 这些将从5点到8点开始 如果我像下面这样使用 for i in (a, b): 这些将打印5和8 现在我需要你的帮助,如果a=5和8意味着我需要找到5到8之间的范围是1到4,循环1到4的形式 如果a=3和5意味着我需要找到3到5之间的范围是1到3,循环1到3的形式。范围a,b返回b-a值的列表,而a,b是只有两个值的元组 为了解决你的问题,你可以这样做 for x in range(a, b + 1

a=5,b=8

我需要在for循环中使用这些数字

for i in range(a, b):
这些将从5点到8点开始

如果我像下面这样使用

for i in (a, b):
这些将打印5和8

现在我需要你的帮助,如果a=5和8意味着我需要找到5到8之间的范围是1到4,循环1到4的形式

如果a=3和5意味着我需要找到3到5之间的范围是1到3,循环1到3的形式。

范围a,b返回b-a值的列表,而a,b是只有两个值的元组

为了解决你的问题,你可以这样做

for x in range(a, b + 1):  # +1 to include the end of the range in the list
    x = x - a + 1;  # Make x start at 1
    ...

或者正如深空公司所指出的那样

for x in range(b - a + 1):  # +1 to include the end of the range
    x = x + 1  # Since range should start at 1
    ...

我想他要求I的值从0到数字之间的差值。对于你在问题中所描述的,我认为你的解决方案对于范围a,b+1中的I应该是这样的。根据我的问题,使用范围a,b+1有什么意义?范围b-a是一个更简单的选择或范围1,b-a+1包括1和b-a,先生,我在a,b和x=x-a+1;范围内用x表示很多;。它给solution@DeepSpace:那将从0@Jahid我的评论中有一个或部分。@DeepSpace:range1,b-a+1必须是range1,b-a+2,否则根据OP的要求,这也是错误的。
for x in range(b - a + 1):  # +1 to include the end of the range
    x = x + 1  # Since range should start at 1
    ...