Python Forum
problem in dictionary .get()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem in dictionary .get()
#1
Hey.. I was writing the code for calculating protein net charge
when I am providing in input the available keys it is working fine.. However, when key is not available it is giving error.. although I have provided the error statement in .get(, 'my error msg')
How to get my error message..
Is it because of print command?
# calculating charge of proteins
prot_seq = input('please put the protein sequence here = ').upper()
print(prot_seq)
aa_charge = {'C':-0.405, 'D':-0.999, 'E':-0.998, 'K':1, 'R':1}
net_charge = 0
for i in prot_seq:
    net_charge += aa_charge.get(i, 'the sequence is not valid')
print(net_charge)
Reply
#2
You should include the full error traceback along with your code, but I'm gonna assume that you're getting a TypeError.

This happens because you're trying to add a string ('the sequence is not valid') to a number (net_charge).
Python doesn't know how to add these up, so an exception is thrown.

Since you need the sequence to be valid, I wouldn't use .get() at all, and would just check if the characters are valid instead.
Reply
#3
(Aug-26-2019, 05:00 AM)roseojha Wrote: although I have provided the error statement in .get(, 'my error msg')

Also, note that second argument of dict.get() is not custom error message. The second argument is default value that is returned when key is missing.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
This is how i would do it:
aa_charge = {'C':-0.405, 'D':-0.999, 'E':-0.998, 'K':1, 'R':1}

while True:
    prot_seq = input('Please input protein sequence: ').upper()
    if all(letter in aa_charge.keys() for letter in prot_seq):
        break
    else:
        print(f'Repeat, possible letters are {"".join(aa_charge.keys())}')
print(prot_seq)

net_charge = sum(aa_charge[letter] for letter in prot_seq)
print(f'Net charge = {net_charge}')
Reply
#5
aa_charge = {'C':-0.405, 'D':-0.999, 'E':-0.998, 'K':1, 'R':1}
prot_seq = input('please put the protein sequence here = ').upper()
try:
    net_charge = sum(aa_charge[aa] for aa in prot_seq)
except KeyError:
    print('the sequence is not valid')
else:
    print(net_charge)
you can use try/except. in the except block you can raise KeyError with your own message
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
I like buran's try...except, especially for using else Smile

Just another idea: determine whether all keys are present by using set:

if set(prot_seq).issubset(aa_charge):
    # calculate sum
else:
    # display message
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem coverting string data file to dictionary AKNL 22 6,273 Mar-10-2020, 01:27 PM
Last Post: AKNL
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,018 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  Problem using input in Dictionary.. Help roseojha 1 1,748 Aug-25-2019, 08:51 AM
Last Post: ThomasL

Forum Jump:

User Panel Messages

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