0

I am trying to solve the problems at http://rosalind.info/problems/list-view/ and there is a problem called IPRB.In short, we have 3 different organisms k(homozygote dominant), m(heterozygote) and n(homozygote recessive). It asks you to calculate the probability of an offspring of two randomly chosen individuals to have a dominant allele. You can check it from here (http://rosalind.info/problems/iprb/).

My problem is that; I achieved to write a working code with example input (2, 2, 2 for k, m, n)and I get the estimated output. However, when the input values are different(i.e 25, 20, 18) from each other, I got an error message as follows:

Traceback (most recent call last): File "C:/Users/mNm/PycharmProjects/PySummer/venv/Scripts/IPRB.py", line 43, in pr3 = float(pr31 + pr32 + pr33) NameError: name 'pr31' is not define

I have researched online the causes of that code. It seems like this error is about name definitions like define after usage, or define out of function, etc. But I didn't have any of them as I checked. In short, I have no idea how to fix it and I decided to ask here.

k = 2
m = 2
n = 2

whole = k + m + n
org = [k, m, n]

I have 3 variables and a list to hold them. That inputs work fine but...

For example when it is like:

k = 25
m = 25
n = 19

whole = k + m + n
org = [k, m, n]

I get the error:

Traceback (most recent call last): File "C:/Users/mNm/PycharmProjects/PySummer/venv/Scripts/IPRB.py", line 43, in pr3 = float(pr31 + pr32 + pr33) NameError: name 'pr31' is not define

and line 43 is here:

    if i == n:
        n_start = float(n / whole)
        for j in org:
            if j == k:
                pr31 = float(n_start * (k / (whole - 1)))
            if j == m:
                pr32 = float(n_start * (m / (whole - 1)) * 0.5)
            if j == n:
                pr33 = float(n_start * ((n - 1) / (whole - 1)) * 0)

43 --> pr3 = float(pr31 + pr32 + pr33)

I want to paste my full code too if you want to see the whole thing and examine it.

k = 2
m = 2
n = 2

whole = k + m + n
org = [k, m, n]

for i in org:
    if i == k:
        k_start = float(k/whole)
        for j in org:
            if j == k:
                pr11 = float(k_start * ((k - 1) / (whole - 1)))
            if j == m:
                pr12 = float(k_start * (m / (whole - 1)))
            if j == n:
                pr13 = float(k_start * (n / (whole - 1)))

    pr1 = float(pr11 + pr12 + pr13)

    if i == m:
        m_start = float(m/whole)
        for j in org:
            if j == k:
                pr21 = float(m_start * (k / (whole - 1)))
            if j == m:
                pr22 = float(m_start * ((m - 1) / (whole - 1)) * 0.75)
            if j == n:
                pr23 = float(m_start * (n / (whole - 1)) * 0.5)

    pr2 = float(pr21 + pr22 + pr23)

    if i == n:
        n_start = float(n / whole)
        for j in org:
            if j == k:
                pr31 = float(n_start * (k / (whole - 1)))
            if j == m:
                pr32 = float(n_start * (m / (whole - 1)) * 0.5)
            if j == n:
                pr33 = float(n_start * ((n - 1) / (whole - 1)) * 0)

    pr3 = float(pr31 + pr32 + pr33)

dom_pr = float(pr1 + pr2 + pr3)

print(dom_pr)

I expect an output between 0-1 since it is a probability but what's more important is to learn what caused that error and what have I done wrong.

  • For example, `p21`, `p22` and `p23` are defined if and only if `i == m`, right? However, `pr2 = float(pr21 + pr22 + pr23)` is calculated _in any case_, even when `i != m`. So, what will happen when you try to add some variables that... _don't exist_? A ` NameError`. – ForceBru Aug 10 '19 at 16:23
  • That was actually correct. I am a novice so I missed that point. Thanks a lot. If you write this as an answer I'll check it as the correct answer. – Mehmet İzmirli Aug 11 '19 at 18:45

1 Answers1

1

This looks OK to me, but if you are still facing an error, try to cover the i loop inside the try catch block.

try:
    k = 25
    m = 20
    n = 19
    whole = k + m + n
    org = [k, m, n]
    for i in org:
        if i == k:
        k_start = float(k/whole)
        for j in org:
            if j == k:
                pr11 = float(k_start * ((k - 1) / (whole - 1)))
            if j == m:
                pr12 = float(k_start * (m / (whole - 1)))
            if j == n:
                pr13 = float(k_start * (n / (whole - 1)))

    pr1 = float(pr11 + pr12 + pr13)

    if i == m:
        m_start = float(m/whole)
        for j in org:
            if j == k:
                pr21 = float(m_start * (k / (whole - 1)))
            if j == m:
                pr22 = float(m_start * ((m - 1) / (whole - 1)) * 0.75)
            if j == n:
                pr23 = float(m_start * (n / (whole - 1)) * 0.5)

    pr2 = float(pr21 + pr22 + pr23)

    if i == n:
        n_start = float(n / whole)
        for j in org:
            if j == k:
                pr31 = float(n_start * (k / (whole - 1)))
            if j == m:
                pr32 = float(n_start * (m / (whole - 1)) * 0.5)
            if j == n:
                pr33 = float(n_start * ((n - 1) / (whole - 1)) * 0)

                pr3 = float(pr31 + pr32 + pr33)

                dom_pr = float(pr1 + pr2 + pr3)
except Exception as e:
    dom_pr=None
    print(e)
print(dom_pr)
Russ J
  • 828
  • 5
  • 12
  • 24