I tried writing different function variations but my count_dna function keeps returning zeroes (despite having consecutive sequences of STRs in the dna sequences)--can anyone please tell me why?

Note: I'm yet to write the part where the program looks for matches in the persons DNA database.


import csv  #  Import necessary modules
import sys
import cs50
import re

track_str = {}  # dictionary to track STRs

def count_dna(strs, dna_sequence):
    i = 0
    while strs * (i+1) in dna_sequence:
        i += 1
    return i
if len(sys.argv) != 3:
    sys.exit("Usage: python dna.py data.csv sequence.txt")

with open(sys.argv[1], 'r') as file, open (sys.argv[2], 'r') as text:
    csv_data = csv.DictReader(file, delimiter = ',')
    dna_sequence = text.read()

with open(sys.argv[1]) as names_dna:
    strs = csv.DictReader(names_dna)
    strs = strs.fieldnames[1:]

""" Turn strs iteratible object to dictionary """
for element in strs:  # Iterate through the single row extracted from csv.reader object into dictionary as KEYS with value = 1
    track_str[element] = 0
for i in track_str:
    string = str(track_str[i])
    track_str[i] = count_dna(string, dna_sequence)


1 Answer 1


for i in dict: iterates through the keys (i.e. i is a key). dict[key] returns the value. So what is string set to here string = str(track_str[i])?

  • 1
    Thank you! This solved it: for key in track_str: track_str[key] = count_dna(key, dna_sequence) "The computer does what you type, not what you think. Real troubleshooting will begin as soon as you stop believing the computer is wrong and you are right." 👍 Nov 3, 2021 at 20:23
  • Thanks. That made my day :) Nov 3, 2021 at 20:29
  • Same for your answer! And it was just enough to get me thinking in the right direction (out of stuckville). Nov 3, 2021 at 23:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .