Problem using input in Dictionary.. Help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Problem using input in Dictionary.. Help (/thread-20675.html) |
Problem using input in Dictionary.. Help - roseojha - Aug-25-2019 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) RE: Problem using input in Dictionary.. Help - ThomasL - Aug-25-2019 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) |