Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While Loop Problem
#1
Hi,
i'm very very new to python, but familiar with vba, so understand how basics work and in this case a loop.

I have the following code:

i = 1
num_page = 1
bar = progressbar.ProgressBar(max_value=response['meta']['total'], widgets=widgets).start()
print("Total number of records : ", response['meta']['total'])
while i <= response['meta']['total']:
...more code here to retrieve records...

bar.update(i)
        i = i + 1
Run ok, but then I get an error:
Error:
Total number of records : 494 [Elapsed time: 0:06:18] |**********************************| (ETA: 00:00:00) Traceback (most recent call last): File "C:\Users\Admin\Downloads\patients_wip.py", line 118, in <module> bar.update(i) File "C:\Users\Admin\AppData\Local\Programs\Python\Python311\Lib\site-packages\progressbar\bar.py", line 672, in update raise ValueError( ValueError: Value 495 is out of range, should be between 0 and 494 [Elapsed time: 0:06:19] |**********************************| (Time: 0:06:19)
I've tried changing from
while i <= response
to
while i < response

I have made it print at each i, and it will print up to the total number of records.

Any advice and guidance would be greatly appreciated.
Reply
#2
Which of the many progressbar packages are you using?

Python indexing starts with 0, not 1. "i" should be initialized to 0, not 1. You are skipping doing something with your first record.

But there is something else going on that you aren't showing. How is "i" getting to 495 if there are 494 records? Does response['meta']['total'] change inside the loop? Is there another place where "i" is changed inside the loop?

It is unusual seeing a while loop in Python. for loops are the loop of choice for a counting loop. I might write your code like this:
bar = progressbar.ProgressBar(max_value=response['meta']['total'], widgets=widgets).start()
print("Total number of records : ", response['meta']['total'])
for i in range(response['meta']['total']):
    . . .
    bar.update(i)
Benno2805 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop reading csv file problem faustineaiden 1 1,578 Dec-11-2021, 08:40 AM
Last Post: ibreeden
  Infinite loop problem Zirconyl 5 3,003 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  Dataframe mean calculation problem: do we have to loop? sparkt 1 2,183 Aug-28-2020, 02:41 PM
Last Post: sparkt
  Python loop problem Kristenl2784 11 5,107 Jun-18-2020, 07:22 PM
Last Post: buran
  Problem with append list in loop michaelko03 0 1,688 Feb-16-2020, 07:04 PM
Last Post: michaelko03
  problem with for loop using integers python_germ 5 3,006 Aug-31-2019, 11:42 AM
Last Post: jefsummers
  problem in loop roseojha 3 2,306 Aug-26-2019, 09:03 AM
Last Post: perfringo
  Nested while loop problem + turtle DreamingInsanity 3 2,980 Jul-06-2019, 02:01 PM
Last Post: DreamingInsanity
  Problem Passing Arguement to do loop stephenmolnar 10 4,864 May-13-2019, 02:56 PM
Last Post: Gribouillis
  Nested for loop strange problem mcva 2 2,631 Mar-16-2019, 12:53 PM
Last Post: mcva

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020