Home > Software design >  Using booleans in dictionary within loop: Second iteration forward gives no values
Using booleans in dictionary within loop: Second iteration forward gives no values

Time:11-24

new to programming here.

In python I'm building up a script that allows one to enter a buffer pH and an amino acid's three letter code. The output should tell you the other amino acids that it should have electrostatic interactions with. The first iteration seems to work, however after that it stops returning any output no matter what parameters are entered. My guess is that somehow the boolean values are being replaced or something. Is there a way for each iteration to start completely fresh? I'd like the user to be able to continue entering parameters and have the output not be influenced at all by the previous iteration.

Here's the code (sorry if it's messy):

aaDic = {
         'Arg': 12.48,
         'Asp': 3.90,
         'Cys': 8.37,
         'Glu': 4.07,
         'His': 6.04,
         'Lys': 10.54,
         'Tyr': 10.46,
         }

while True:

    x = input("Enter pH of buffer: ")
    y = input("Enter three letter code for an amino acid: ")


    if float(x) > float(aaDic[y]):
        ProtonationInput = True
    elif float(x) < float(aaDic[y]):
        ProtonationInput = False
    print("\n")
    print("Is your amino acid,", y, ", protonated?", ProtonationInput, "\n")

    print("At pH", x, y, "likely interacts with the following residues: \n")

    for aa in aaDic.keys():
        if float(x) > float(aaDic[aa]):
          aaDic[aa] = True
        elif float(x) < float(aaDic[aa]):
          aaDic[aa] = False
        #print(aa, aaDic[aa], "\n")


    for aa in aaDic.keys():
      if ProtonationInput == True:
            if aaDic[aa] == False:
              print(aa, "\n")
      elif ProtonationInput == False:
            if aaDic[aa] == True:
              print(aa, "\n")

    continue

Thanks in advance!

CodePudding user response:

Your problem is that you're using the aaDic dictionary to store float values. And then overriding those values with Boolean values. As seen below,

for aa in aaDic.keys():
        if float(x) > float(aaDic[aa]):
          aaDic[aa] = True
        elif float(x) < float(aaDic[aa]):
          aaDic[aa] = False
        #print(aa, aaDic[aa], "\n")

You're code depends on a dictionary of floats and booleans. But your only using one dictionary. So make another dictionary for booleans. Example below,

aaDic = {
         'Arg': 12.48,
         'Asp': 3.90,
         'Cys': 8.37,
         'Glu': 4.07,
         'His': 6.04,
         'Lys': 10.54,
         'Tyr': 10.46,
         }
# Add this new dictionary of booleans
boolAADic = {key:False for key in aaDic }

while True:

    x = input("Enter pH of buffer: ")
    y = input("Enter three letter code for an amino acid: ")


    if float(x) > float(aaDic[y]):
        ProtonationInput = True
    elif float(x) < float(aaDic[y]):
        ProtonationInput = False
    print("\n")
    print("Is your amino acid,", y, ", protonated?", ProtonationInput, "\n")

    print("At pH", x, y, "likely interacts with the following residues: \n")

    print(aaDic)
    for aa in aaDic.keys():
        if float(x) > float(aaDic[aa]):
          boolAADic[aa] = True # Change to boolean dictionary
        elif float(x) < float(aaDic[aa]):
          boolAADic[aa] = False # Change to boolean dictionary
        #print(aa, aaDic[aa], "\n")


    for aa in aaDic.keys():
      if ProtonationInput == True:
            if boolAADic[aa] == False: # Change to boolean dictionary
              print(aa, "\n")
      elif ProtonationInput == False:
            if boolAADic[aa] == True: # Change to boolean dictionary
              print(aa, "\n")



  • Related