最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Python: Values incremented by a user supplied number in each iteration using a loop - Stack Overflow

programmeradmin3浏览0评论

Below is my problem statement: I want to generate the below things using while loop (or for loop)

c    top     skip
1    1000     0
2    1001    2000 --- added 999
3    2001    3000 --- added 999
4    3001    4000 --- added 999
--and so on.

lets assume that c is dynamically set. The requirement is not to create a pandas dataframe but to print the above one by one.

To achieve this, I have applied the below python code:

url = 'http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$'
top = 1000 
skip = 1999
cnt_v = 3
i=1
while i <=cnt_v:
  if i==1:
    print(url + 'top = {}'.format(str(top)) + '&$skip = 0')
    #break
  else:
    skip += top + 1
    print(url + 'top = {}'.format(str(top)) + '&$skip = {}'.format(str(skip)))
    top += top + 1
  i+=1

It is working fine for the first condition (i.e. first row of the above table). But is breaking from 3rd row where skip and top values are incorrectly being populated. Please see below the output:

http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 1000&$skip = 0
http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 1000&$skip = 2000
http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 2001&$skip = 3002
http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 4003&$skip = 6006

Am I missing anything here?

Edit

Following the answer as suggested, I have made the changes as below and it is working fine.

top = 1000
cnt_v = 10
def output(top, skip):
  print(f'{url}{top = }&${skip = }')

for c in range(1, top*cnt_v, 1000):
  if c==1:
    output(top, 0)
  else:
    output(c , c + 999)

Below is my problem statement: I want to generate the below things using while loop (or for loop)

c    top     skip
1    1000     0
2    1001    2000 --- added 999
3    2001    3000 --- added 999
4    3001    4000 --- added 999
--and so on.

lets assume that c is dynamically set. The requirement is not to create a pandas dataframe but to print the above one by one.

To achieve this, I have applied the below python code:

url = 'http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$'
top = 1000 
skip = 1999
cnt_v = 3
i=1
while i <=cnt_v:
  if i==1:
    print(url + 'top = {}'.format(str(top)) + '&$skip = 0')
    #break
  else:
    skip += top + 1
    print(url + 'top = {}'.format(str(top)) + '&$skip = {}'.format(str(skip)))
    top += top + 1
  i+=1

It is working fine for the first condition (i.e. first row of the above table). But is breaking from 3rd row where skip and top values are incorrectly being populated. Please see below the output:

http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 1000&$skip = 0
http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 1000&$skip = 2000
http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 2001&$skip = 3002
http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 4003&$skip = 6006

Am I missing anything here?

Edit

Following the answer as suggested, I have made the changes as below and it is working fine.

top = 1000
cnt_v = 10
def output(top, skip):
  print(f'{url}{top = }&${skip = }')

for c in range(1, top*cnt_v, 1000):
  if c==1:
    output(top, 0)
  else:
    output(c , c + 999)
Share Improve this question edited Feb 15 at 6:58 pythondumb asked Feb 15 at 6:15 pythondumbpythondumb 1,2471 gold badge16 silver badges39 bronze badges 3
  • Why does your desired output look so different from the observed output? – Klaus D. Commented Feb 15 at 6:29
  • @KlausD.: The desired output is purely for representation purpose. Idea is to flow the logic. The observed output is something that I want to generate by following the rules that is given in the table. – pythondumb Commented Feb 15 at 6:43
  • How about you add the real desire output? Like the exact text (by the character) that you want to produce. I guess you don't to get answers that are for representation purposes only. – Klaus D. Commented Feb 15 at 6:46
Add a comment  | 

1 Answer 1

Reset to default 0

Keep it simple. It is a simple progression after the first line:

url = 'http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$'

def output(top, skip):
    print(f'{url}{top = }&${skip = }')

output(1000, 0)
# step "c" by 1000 up to but not including 10000.  Adjust as needed.
for c in range(1000, 10000, 1000):
    output(c + 1, c + 1000)

Output:

http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 1000&$skip = 0
http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 1001&$skip = 2000
http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 2001&$skip = 3000
http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 3001&$skip = 4000
http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 4001&$skip = 5000
http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 5001&$skip = 6000
http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 6001&$skip = 7000
http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 7001&$skip = 8000
http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 8001&$skip = 9000
http://<ip>:<port>/sap/opu/odata/sap/ZRSO2_BKPF?$format=json&$top = 9001&$skip = 10000

c didn't appear to be output so I didn't stick to the 1,2,3... value, but if you need it for some other purpose adjust the linear formula.

发布评论

评论列表(0)

  1. 暂无评论