Python Forum

Full Version: Problem using input in Dictionary.. Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey.. I am trying to calculate protein weight using dictionary where weight is given for each amino acid
As and input I am giving = AALI which should give 440 weight
But I am getting an error that the dictionary is not callable
What I am doing wrong??

prot_seq = input('please write the amino acid sequence = ')
prot_weight = {'A':89, 'V':117, 'L':131, 'I':131, 'P':115,
'F':165, 'W':204, 'M':149, 'G':75, 'S':105,
'C':121, 'T':119, 'Y':181, 'N':132, 'Q':146,
'D':133, 'E':147, 'K':146, 'R':174, 'H':155}
for aa in prot_seq:
weight=sum(prot_weight[aa])
print(weight)
You are overwriting weight for each aa in the input and calculating the sum of an int
so either:
prot_weight = {'A':89, 'V':117, 'L':131, 'I':131, 'P':115, 'F':165, 'W':204,
               'M':149, 'G':75, 'S':105, 'C':121, 'T':119, 'Y':181, 'N':132, 
               'Q':146, 'D':133, 'E':147, 'K':146, 'R':174, 'H':155}
prot_sequence = input('please write the amino acid sequence = ')
weight = 0
for aa in prot_sequence:
    weight += prot_weight[aa]
print(weight)
or do it the pythonic way:
prot_weight = {'A':89, 'V':117, 'L':131, 'I':131, 'P':115, 'F':165, 'W':204,
               'M':149, 'G':75, 'S':105, 'C':121, 'T':119, 'Y':181, 'N':132, 
               'Q':146, 'D':133, 'E':147, 'K':146, 'R':174, 'H':155}

prot_sequence = input('please write the amino acid sequence = ')
weight = sum(prot_weight[character] for character in prot_sequence)
print(weight)