Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have a defined a variable inside a for loop, but when printing it outside of the loop it doesn't print correctly

Here is the code:

count_1 = 0 
count_0 = 0 
list = ('001111011011','000110001010','011010111111')`
for i in list:
index = 0 
y = i[index]

if y == "1":
    count_1 = count_1 + 1
if y == "0":
    count_0 = count_0 + 1

if count_1 > count_0:
    for i in list:
        final_after_1 = []
        if i[0] == "1":
            final_after_1.append(i)
            formatted = (','.join(final_after_1))
if count_0 > count_1:
    for i in list:
        final_after_1 = []
        if i[0] == "0":
            final_after_1.append(i)
            formatted = (','.join(final_after_1))
if count_0 == count_1:
    for i in list:
        final_after_1 = []
        if i[0] == "1":
            final_after_1.append(i)
            print(final_after_1)
            formatted = (','.join(final_after_1))
    

print(formatted)

(Apologies in advance if this question is worded badly, this is my first time asking a question).

This piece of code is working fine except for this one issue. It is designed to identify the first index of each 12-digit number in the list, and then work out if a 1 or 0 is more common in this position. It then selects all the numbers with the more common number in the first position and adds them to a list. I want to print this list at the end of the program.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have defined a variable (called formatted) to be equal to a list of various numbers. When I print it out within the loop I have defined it in, it prints all the numbers that should be in the list, like this:

When I print it outside of the loop as in the code above it returns only the final number:

011010111111

Whereas printing it inside the loop like this:

if count_0 > count_1:
        for i in list:
            final_after_1 = []
            if i[0] == "0":
                final_after_1.append(i)
                formatted = (','.join(final_after_1))
                print(formatted)

does return this full desired list:

001111011011

000110001010

011010111111

Any ideas why this is happening?

>Solution :

Within you loop(s) the value of formatted is updated for each iteration. After the last iteration it is no longer updated and that last value is the output of the last print statement.

A simpler example:

for x in range(100):
    pass//looping over, x is 0..99

print(x)

This will print out 99, the last value held by the variable "x".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading