0

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!

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I guess I never spotted that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.