Python Forum
Please help my while loop does not work as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help my while loop does not work as expected
#6
It exits on some inputs, just not on the ones you expect, I guess.

I think you wanted unit=2, mod=5 to exit. Look at what happens to the values each time through the loop:

Output:
unit=2 mod=5 count=1 x=2 unit=4 mod=5 count=2 x=4 unit=12 mod=5 count=3 x=2 unit=48 mod=5 count=4 x=3 unit=240 mod=5 count=5 x=0 unit=1440 mod=5 count=6 x=0 unit=10080 mod=5 count=7 x=0 unit=80640 mod=5 count=8 x=0 unit=725760 mod=5 count=9 x=0 unit=7257600 mod=5 count=10 x=0 ...
x will never become 1 and this loop will never exit.

I haven't followed the actual algorithm you're trying to recreate, but line 7 seems suspect to me, since it overwrites the unit. Perhaps instead of line 7 changing unit, you want to calculate x as x = (unit * count) % mod?
Reply


Messages In This Thread
RE: Python - by bowlofred - Sep-28-2020, 07:12 PM
RE: Python - by KingKhan248 - Sep-28-2020, 07:29 PM
RE: Python - by bowlofred - Sep-28-2020, 07:49 PM
RE: Please help my while loop does not work as expected - by bowlofred - Sep-28-2020, 08:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  While Loop Does Not Work Properly mactron 4 957 Jun-22-2023, 01:04 AM
Last Post: mactron
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,057 Dec-18-2021, 02:38 AM
Last Post: knight2000
  How can this for loop work without : ? Pedroski55 1 1,726 Dec-13-2020, 01:19 AM
Last Post: palladium
  For loop in my __init__ doesn't work as expected Jessy 2 2,396 Nov-18-2019, 10:07 AM
Last Post: buran
Question Why does modifying a list in a for loop not seem to work? umut3806 2 2,327 Jul-22-2019, 08:25 PM
Last Post: umut3806
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,241 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  for loop just work one Faruk 1 2,034 Jan-19-2019, 05:34 PM
Last Post: Larz60+
  'Looping' does not work out within a 'for Loop' Placebo 4 3,382 Sep-15-2018, 08:19 PM
Last Post: Placebo
  Having issues getting a loop to work PLESSE HELP ASAP manthus007 1 2,144 Aug-25-2018, 10:44 AM
Last Post: j.crater
  Scoping Question: If else in for loop not evaluating i as expected datasundae 3 3,249 May-25-2018, 06:43 PM
Last Post: datasundae

Forum Jump:

User Panel Messages

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