Dictionary counter
<p>Let's say I have a sequence "ADFG"
The script should see if it's in the list (A,T,G,C) and then if it is, get the key from the dictionary and add 1.
So, for the sequence "ADFG", it would add 1 to variable "a" and to variable "g", and add 2 to variable "error".
However, while the script isn't returning an error, the dicio[i]+=1 line isn't adding up anything.... Any ideas?
I know there are simpler ways to do this but I would like to use a dictionary.</p>

<pre><code>seq= input ("dna seq:").upper()  
a,t,g,c,error=0,0,0,0,0
dicio= {"A":a,"T":t,"G":g,"C":c}
for i in seq:
    if i in ("A","T","G","C"):
        dicio[i]+=1
        a+=1
    else:
        error+=1

print("A={:.2f} %".format(a/len(seq)*100))
print("T={:.2f} %".format(t/len(seq)*100))
print("G={:.2f} %".format(g/len(seq)*100))
print("C={:.2f} %".format(c/len(seq)*100))
print("Da sequencia,{:.2f} % sao erros".format(error/len(seq)*100))
</code></pre>

<p>you mean that t,g,c are unaffected by the adding in <code>dicio</code></p>

<p>That's normal: dictionary has not taken the references of the variables, but just copied the initial values. You're not updating them when you add 1 to the values of the dictionary.
(<code>a</code> is a special case in your code, it gets updated in all cases, but maybe it's a bug, at least not logical compared to the rest of the code).</p>

<p>To do what you want, you need to forget about your <code>t,g</code> ... variables and just use <code>dicio</code> to display the results:</p>

<pre><code>for i in ("A","T","G","C"):
   print("{}={:.2f} %".format(i,dicio[i]/len(seq)*100))
</code></pre>

<p>(as a bonus you'll save a lot of copy/paste :))</p>

<p>BTW: If you want to initialize the dictionary and keep keys ordered you need <code>ordereddict</code>:</p>

<pre><code>from Collections import ordereddict

dicio = ordereddict()
for k in ("A","T","G","C"):
   dicio[k] = 0
</code></pre>

python python-3.x dictionary count