Stuck in a loop

H

hexusnexus

I wrote a simple algorithm and it keeps getting stuck in a loop. I
guess I'm just to tired to figure it out:

compcount=[5,4,2,2]
suitrank=[0,0,0,0]

trump=2
l,lt=0,0
while l<4:
while lt<4:
if l==trump:
l+=1
if l>3:
break
if lt==trump:
lt+=1
if compcount[l]<compcount[lt]:
suitrank[l]+=1
lt+=1
l+=1

In case you're wondering, the point is to rank suits from highest to
lowest based on how few cards each suit has. I hope that's enough
information.
 
D

Dan Bishop

I wrote a simple algorithm and it keeps getting stuck in a loop. I
guess I'm just to tired to figure it out:

compcount=[5,4,2,2]
suitrank=[0,0,0,0]

trump=2
l,lt=0,0
while l<4:
while lt<4:
if l==trump:
l+=1
if l>3:
break
if lt==trump:
lt+=1
if compcount[l]<compcount[lt]:
suitrank[l]+=1
lt+=1
l+=1

In case you're wondering, the point is to rank suits from highest to
lowest based on how few cards each suit has. I hope that's enough
information.

Inside the inner loop, lt never changes if lt != trump, so you get an
infinite loop the first time when lt == 0.

I think you may have misindented the last two lines.
 
T

Terry Reedy

|I wrote a simple algorithm and it keeps getting stuck in a loop. I
| guess I'm just to tired to figure it out:

The easiest way to figure out somethinglike this is to print your variables
from inside the loop to see things stick, or if there is a cycle.

| compcount=[5,4,2,2]
| suitrank=[0,0,0,0]
|
| trump=2
| l,lt=0,0
| while l<4:
| while lt<4:

print l, lt

| if l==trump:
| l+=1
| if l>3:
| break
| if lt==trump:
| lt+=1
| if compcount[l]<compcount[lt]:
| suitrank[l]+=1
| lt+=1
| l+=1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top