Draxdo
Draxdo

Reputation: 5

Python 'While' loop freezes up Ubuntu Linux

I am programming python just chilling and challenging myself when I stumble across an issue in my code! When I run it it freezes up my computer. The code includes nested while loops so I don't know if that has anything to do with it but here is the code:

def rand_noise_map(high_frequency, low_frequency):
    layer = ''
    mega = ''
    import random
    i = 0
    while i < 6:
        n = 0
        while n < 6:
            var = random.randint(0, 1)
            if var == 0:
                layer = layer + high_frequency
            else:
                layer = layer + low_frequency
            n = n + 1
        layer = layer + '\n'
        mega = mega + layer
        i = i + 0
    return mega


print(rand_noise_map('H', 'L'))

Anyway the code freezes up my computer and I have no idea why. It uses nested while loops so that might be a factor but otherwise I have no idea. Thank you in advance!

Upvotes: 0

Views: 243

Answers (1)

drew
drew

Reputation: 473

You've created an infinite loop.

Because you have i = i + 0 at the bottom of your outer loop, the outer loop never terminates, meaning that it'll run forever.

The fast fix is to change that zero to a one.

The more pythonic way is to change how you do your loops:

for i in range(6):
    for n in range(6):
        # inner loop code here
    # outer loop code here
return mega

Upvotes: 4

Related Questions