[Python] Python: Values incremented by a user supplied number in each iteration using a loop

Discussão em 'Python' iniciado por Stack, Fevereiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página