Search code examples
pythonif-statementwhile-loopbreakpoints

While loops and breaking when boolean becomes false


I am trying to make a loop whereby the program keeps on going until bank>100000, and within that while loop there is another while loop which determines whether bank has reached a minimum(whether user has failed, and bank resets to 20000). Currently the loop runs and continues forever even if successive iterations reach the 1000000 condition. Can anyone see a way of resolving this?

n.b. 'singles' is just an array containing one number.

Many thanks.

i = 0
newbank = []
iterations = []
while bank < 1000000:
    bank = 20000
    while bank > 100:
        spin = randint(0, 38)
        if choices2 == singles:
            if spin == singles:    
                bank = bank + (bet * 35)
            else:
                bank = bank - bet
            i = i + 1
            iterations.append(i)
            newbank.append(bank)

Solution

  • Your second while continues to run even if bank < 10000000, so you'll never leave that loop, and if you do, well, you'll stay on the first loop.

    There is no way your code can stop.

    Do you want something like...

    while bank > 100 and bank < 1000000:
    

    I don't know exactly what logic you're trying to achieve, but you need to think better about your stopping conditions.

    Edit:

    Upon reading your question better, I think you should get rid of the two loops, since one will be more than enough.

    Initialize your bank = 20000 outside the loop,

    bank = 20000
    while bank < 1000000:
        if bank < 100:
            bank = 20000
    
    ....