Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I aim to create a dynamic range of a python loop. I know once the end range is computed only once, when it is passed as an argument to the range generator. However, this an example:

i = 1

for i in range(1,i,1):

    print i

    i = i +1

it is obvious that with i=1 the loop is skipped. But I want somehow that this range is dynamically changing according to i parameter.

This is my case where I want to use a dynamic range:

I calculate the capacity of a link

I calculate the bandwidth on that link

I do a loop with increasing of traffic sent in which should be equal to the capacity, I call it overload value.

The increasing is of range starts from 1 to the overload value.

This overload value is being calculated every time in each iteration, and it updates the range. If say theoretically the overload value is 20, then the range goes until 20.

This is my code:

capacity = Router_1.get_tunnel_capacity()

tunnel_bandwidth = Router_1.check_bandwidth_overload()

    if tunnel_bandwidth <= capacity:

        for bandwidth in range(1, range_end, 1):

            os.system('iperf -c ' + server_address + ' -u -p 50001 -b ' + str(bandwidth) + 'M -i 1')

tunnel_bandwidth = Router_1.check_bandwidth_overload()

if tunnel_bandwidth <= capacity:

   # update the range_end according to tunnel_bandwidth

range_end is the dynamic value of the range. Is there anyway to make it dynamic?

1 Answer

0 votes
by (25.1k points)

You should use a while loop instead of a for loop:

x = 0

range = 10

while x < range:

    print(x)

    if x == 9:

        range = 20

    x+= 1

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
4 answers
asked Apr 6, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Dec 13, 2020 in Python by ashely (50.2k points)

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...