Python Forum
Problem using input in Dictionary.. Help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem using input in Dictionary.. Help
#1
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)
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in using input command akbarza 4 998 Oct-19-2023, 03:27 PM
Last Post: popejose
  problem in entering address of a file in input akbarza 0 619 Oct-18-2023, 08:16 AM
Last Post: akbarza
  Problem with input after function luilong 10 4,020 Dec-04-2021, 12:16 AM
Last Post: luilong
  Problem restricting user input in my rock paper scissors game ashergreen 6 4,501 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  single input infinite output problem Chase91 2 1,908 Sep-23-2020, 10:01 PM
Last Post: Chase91
  How do you replace a dictionary key with a new input? thewetmosquito 4 3,202 Aug-23-2020, 03:48 AM
Last Post: perfringo
  Problem with the input marios 4 2,006 May-03-2020, 01:01 PM
Last Post: marios
  User input & Dictionary tfernandes 5 3,592 Apr-03-2020, 07:12 PM
Last Post: tfernandes
  problem coverting string data file to dictionary AKNL 22 6,270 Mar-10-2020, 01:27 PM
Last Post: AKNL
  Get a value from a dictionary through input Anony 3 2,240 Jan-26-2020, 06:18 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020