简体   繁体   中英

If statement won't run even though conditions are met

I am doing a project where my code should take in lines from text files, and read the first digit of each value in a table. It should then apply Benford's Law to see the frequency of the first digit of the values 1-9. Right now I am taking the values and putting them into a list called 'lines' then go to a for loop with range(1,10) with the value as n to check values 1-9 and then nest another for loop in it that iterates through all the elements in line and then I nest a if statement within that one that checks if the first digit of the element in lines is the same as n. If it is put the digit every time it occurs into a list called number_counter. I use another for loop later to convert them into frequencies but the if statement that checks to see if the first digit of the element within lines and n are equal never runs though even though I printed out n and the first digit everytime to see if they ever matched up and they did multiple times. I am confused onto why it is not triggering. The code is far from being cleaned up and organized as I am still working on the project.

lines = []

file = open('data1.csv', 'r')
f = file.readlines()
for line in f:
    lines.append(line.strip())
file.close()

r = open('analysis_results.txt', 'w')

number_counter = []
k = 0
for n in range(1,10):
    for x in lines:
        k = x[0]
        if k == n:
            number_counter.append(k)

print(number_counter)

frequencies = []

for n in range(1,10):
    p = 0
    print(n)
    for x in number_counter:
        if x == n:
            p += 1       
    p = p // len(lines)
    frequencies.append(p)
     
print(frequencies)
print(k)

There are a number of errors in your code. First there is a type mismatch when you try to compare k with n in your first loop. k is becoming a string when you assign x[0] to it, and n is an integer so they will never be equal.

Also when you count the frequency you always do p = p // len(lines) which most of the time will always be zero and if all the lines start with the same number between 1-10 it might be one for the case of that specific number.

So all in all try this modified version:


lines = []

file = open("data.csv", "r")
f = file.readlines()
for line in f:
    lines.append(line.strip())
file.close()


number_counter = []
# k = 0  # <-- you don't need this at all
for n in range(1, 10):
    for x in lines:
        k = int(x[0])  # convert the first character to int
        if k == n:
            number_counter.append(k)

print(number_counter)

frequencies = []

for n in range(1, 10):
    p = 0
    print(n)
    for x in number_counter:
        if x == n:
            p += 1
    # p = p // len(lines)  # remove this to get the integer number of occurences
    frequencies.append(p)

print(frequencies)
print(k)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM