r/pythonhelp • • Oct 06 '21

Problem nested loop in a personal challenge project (my code).

I am having a problem with my nested loop in this code I am playing with. This is not for class, but a personal quest. The goal is to random generate sets of 8 bits based on how many characters (ASCII) they want and then save to file output to "random.txt". The problem area begins with the "else:" statement and ends before "finally:", The problem is that it executes 8 iterations of the inner loop and once of the outer loop so I always get 8 bits.

Example of desired output of random.txt:

01001000 01100101 01101100 01101100 01101111 00100000 01010010 01100101 01100100 01100100 01101001 01110100 00100001

Current code:

import random

def generate():
    random_num = random.randint(0,1)
    return random_num

def main():
    try:
        output = open("random.txt", "w")

        num_of_num = int(input("How many characters should the file hold?"))

    except Exception as PotentialError:
           print("An error has occured.", PotentialError)

    else:
        for num_count in range(0, num_of_num + 1):
            for bits in range (1, 7 + 1):
                random_num = generate()
                output.write(str(random_num))
            return bits
        return num_count

        print(num_of_num, "numbers have been written to the file random.txt")

    finally:
            output.close()
main()
2 Upvotes

8 comments sorted by

2

u/[deleted] Oct 06 '21

After your inner loop you return the number of iterations, which immediately exits the for loop. You also return after the outer for loop before you hit your print statement.

1

u/[deleted] Oct 06 '21

Took the returns out and was successful. What is the purpose of the return?

3

u/carcigenicate Oct 06 '21

To leave the function and at the same time return some data back to whoever called the function.

1

u/[deleted] Oct 06 '21

Why would adding a return prevent the execution of further iterations, does that force the loop to exit regardless??

2

u/carcigenicate Oct 06 '21

return causes the function to exit regardless of anything else like loops that it may be in. You could use yield to yield multiple values, but I wouldn't use that until you're more familiar with functions and returning.

1

u/[deleted] Oct 06 '21

Thanks for your mentorship and assistance! This is the furthest I have gotten with a modern programming/scripting language. Legit file I/O and functions.

2

u/CraigAT Oct 06 '21

Different to how I would structure it.

A while loop for validation of the input (say number has to be > 0 and <100)
..Get the number of characters in the file, using "input"
Then open the file using a "with" statement
Start an outer loop for the number of characters in the file
..Start an inner loop for the 8 digits ....Generate the 1 or 0
..End inner loop
..Add a space to separate the characters
End outer loop
Close the file

Edit: I hate Reddit's code formatting

1

u/[deleted] Oct 06 '21 edited Oct 07 '21

Using everyone's advice this is what I came to, 7 bit ASCII, and 8 bit Extended ASCII works too if I code the loop to be 1 - 8 ending on the 8th iter.

else:

for num_count in range(0, num_of_num + 1):

for bits in range (1, 7 + 1):

random_num = generate()

output.write(str(random_num))

output.write(str(" "))

print(num_of_num, "numbers have been written to the file random.txt")

Is there an efficient way just to convert the 8 bits presented as a string as an UTF8/ASCII char from an input such as "10010000" (NOP). And then write the respective character or "" I can take the spaces out if that is an issue, and don't mind control chars.

I know, guys. "Give a mouse a cookie and he will want some milk."

Example output of current working code:

1100110 0000100 0010110 0010000 1010001 0110011 0111100 0101000 0011001 1110101 0011101 0000101 1100001 1000111 1110101 0011011 0110110 1110101 0100100 1010101 1111011 1001010 0010100 1001000 1111011 0001101 0111101 0110101 0110110 0100111 0100001 0111111 0011010 0100111 1010011 0001001 0000111 1110111 0110011 1010000 1011010 1111110 1000011 1011100 1101000 1100001 1001100 0101000 1110001 1011110 1000101 0110010 0000010 1111101 1101000 0101110 1001000 1101110 0001100 0011100 1111000 0000011 0100101 0101110 0111110 0100001 1000111 1101111 0101001 1110111 1000111 1010011 1011011 1001110 1011101 0100110 1111111 1000001 1010111 0010011 1100000 0000001 0101100 0001101 1010000 1111100 0000100 0000000 1001110 1110101 1001100 1001010 0111000 0011011 0001001 1010000 0110001 1010101 0000010 0111110 0001100 1011100 0010001 0101010 1011001 0000100 1111111 1111010 0010000 0110110 0001001 1101000 1101010 1010001 1001000 1100100 1010111 1000011 0001110 1000111 0010111 0100100 1010001 1111000 1101011 1110011 1011110 1110001 1101011 0000100 0100011 1100001 1100111 0111011 1000011 0111011 1100000 0110011 0110110 1110010 1011010 0100101 0101110 1001001 1110101 1101110 0111010 0101100 1101001 1010100 0000100 1011101 1010100 0110101 0101001 0010100 0110100 0001110 0101010 1000111 1100111 0110101 0100110 1101011 1001010 0100101 1111010 1001001 1110100 1001011 0000000 0011110 0001111 1011111 0000010 0001111 0111011 0111111 1110101 1100000 1101011 1010011 0011001 0110011 1100001 0110101 1110011 1001011 1000011 0010010 0010000 1110110 1010011 1010100 1100011 0111011 1100111 1011010 0010110 0000110 1011001 0100101 1001000 0010100 1010110 0011101 1001110 1110110 1111100 0000001 1111010 0000010 0111010 0111011 0110100 1001010 0001110 1011100 0101010 0101011 1111100 1111011 1111000 0000100 1001101 1001000 1001001 0001011 0001011 1111001 0000011 1100111 1011000 0011010 1100000 1100011 0100001 1101001 0101001 1110010 0001010 1110001 1000010 1101101 1001000 1111010 1011010 1101111 0010010 1100010 0001110 0010110 0100110 0001011 0010111 1101000 1011000

New goal output which will be gibberish, and that's perfect:

fQ3<(uaGu6u$U{JH{

=56'!?'S w3PZ~C\haL(q^E2}h.Hn x%.>!Go)wGS[N]&AW`,

P|NuLJ8 P1U> \*Yz6 hjQHdWCG$Qxks^qk#ag;C;`36rZ%.Iun:,iT]T5)4*Gg5&kJ%zItK_;?u`kS3a5sKCvSTc;gZY%HVNv|z:;4J\*+|{xMHI ygX`c!i)r

qBmHzZob& hX

I used a website to produce the output, but would like to do so indigenously.