Python Forum
UnboundLocalError: local variable ' ' referenced before assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UnboundLocalError: local variable ' ' referenced before assignment
#1
I just cant find the problem here. I have searched for it. but that did not solve my problem.

def fentanyl():    # This one Works
    vaegt = int(input("Hvad vejer patienten? "))
    max_dosis = vaegt * 4
    max_volumen = vaegt * 4 * 2 / 100
    print(f"Du må max total give ptt. {max_dosis} mikrogram, sv. {max_volumen} ml")
    mk_kg = float(input("Hvor mange mikrogram pr. kg. vil du give?  "))
    mk_kg *= vaegt
    dosis = mk_kg * 2 / 100
    print(f"Du skal give {mk_kg} mikrogram")
    print(f"Du skal give {dosis} ml.")

def adrenalin(): #this does not work
    vaegt = int(input("Hvad vejer patienten? "))
    tilbage = vaegt % 5
    vaegt -= tilbage
    if tilbage > 2:
        vaegt += 5
        dosis = vaegt * 0.01
    if vaegt >= 50:
        print("Du skal give 0.5 mg, sv. 0.5 ml!")
    else:
        print(f"Du skal give {dosis} mg, sv. {dosis} ml!")    # It says here that UnboundLocalError: local variable 'dosis' referenced before assignment

def midazolam():    # This does not Work
    vaegt = int(input("Hvad vejer patienten? "))
    tilbage = vaegt % 5
    vaegt -= tilbage
    if tilbage > 2:
        vaegt += 5  #### problemer når den udregner med præcis 13 kg!!!!!!!!!!!!!! #Only note to myself, dont think about this
    dosis = vaegt * 0.2
    ml = dosis * 2 / 10
    if vaegt >= 50:
        print("Giv 10 mg, sv. 2 ml intranasalt!")
    else:
        print(f"Du skal give {dosis} mg, sv. {ml} ml!")

medicin = (input("Hvilken medicin vil du give? "))     
if medicin.lower() in ("fentanyl", "fen", "fenta", "fe"):
    fentanyl()
elif medicin.lower() in ("adrenalin", "ad", "adre"):
    adrenalin()
elif medicin.lower() in ("midazolam", "mi", "mida"):
    midazolam()
Reply
#2
show full traceback in error tags

note that when tilbage <= 2 then if body (lines 17-18) will not be executed and dosis is not defined.
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
#3
(Sep-03-2019, 03:28 PM)buran Wrote: show full traceback in error tags

note that when tilbage <= 2 then if body (lines 17-18) will not be executed and dosis is not defined.

opsie daisy...
thak you very much.. I did not see that. Im new to programming. Glad you will help.
I have solved it this way. Is that a good way to solve it.

def adrenalin():
    vaegt = int(input("Hvad vejer patienten? "))
    tilbage = vaegt % 5
    vaegt -= tilbage
    if tilbage > 2:
        vaegt += 5
        dosis = vaegt * 0.01
        print(f"Du skal give {dosis} mg, sv. {dosis} ml!")
    elif tilbage <= 2:
        dosis = vaegt * 0.01
        print(f"Du skal give {dosis} mg, sv. {dosis} ml!")
    if vaegt >= 50:
        print("Du skal give 0.5 mg, sv. 0.5 ml!")
Reply
#4
why not use same approach like in the other function - midazolam() where dosis calculation and print function are outside the if block altogether?

that said both functions look virtually the same - you can use just one function? You can pass an argument to distinct between use cases. Probably all 3 could be refactored

given names and text not in english it's difficult to grasp the use cases (it's medicine dose obviously)
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
#5
def fentanyl():
    weight = int(input("Hvad vejer patienten? "))
    max_dosis = vaegt * 4
    max_volumen = weight * 4 * 2 / 100
    print(f"Du må max total give ptt. {max_dosis} mikrogram, sv. {max_volumen} ml") #max amount total to be given
    mk_kg = float(input("Hvor mange mikrogram pr. kg. vil du give?  ")) # amount to be given per kilo in mikrograms
    mk_kg *= weight
    dosis = mk_kg * 2 / 100 
    print(f"Du skal give {mk_kg} mikrogram") #micrograms to be given
    print(f"Du skal give {dosis} ml.") #volumen to be given

def adrenalin():
    weight = int(input("Hvad vejer patienten? ")) #what does the patient weight
    left = weight % 5 
    weight -= left
    if weight >= 50:
        print("Du skal give 0.5 mg, sv. 0.5 ml!")
    elif left > 2:
        weight += 5
        dosis = weight * 0.01
        print(f"Du skal give {dosis} mg, sv. {dosis} ml!")
    elif left <= 2:
        dosis = weight * 0.01
        print(f"Du skal give {dosis} mg, sv. {dosis} ml!")


def midazolam():
    weight = int(input("Hvad vejer patienten? "))
    left = weight % 5
    weight -= left
    if weight >= 50:
        print("Giv 10 mg, sv. 2 ml intranasalt!")
    elif left > 2:
        weight += 5
        dosis = weight * 0.2
        ml = dosis * 2 / 10
        print(f"Du skal give {dosis} mg, sv. {ml} ml!")
    elif left <= 2:
        dosis = weight * 0.2
        ml = dosis * 2 / 10
        print(f"Du skal give {dosis} mg, sv. {ml} ml!")


medicin = (input("Hvilken medicin vil du give? "))
if medicin.lower() in ("fentanyl", "fen", "fenta", "fe"):
    fentanyl()
elif medicin.lower() in ("adrenalin", "ad", "adre"):
    adrenalin()
elif medicin.lower() in ("midazolam", "mi", "mida"):
    midazolam()
this is how I solved it for now. if there is a better way. please help with some exampels. or hints. in easily understandeble english.
So far, thank you very mush for your help. :D
Reply
#6
one thing I think is wrong is left = weight % 5
Do you know what modulo operator does?
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
#7
(Sep-03-2019, 05:51 PM)buran Wrote: one thing I think is wrong is left = weight % 5
Do you know what modulo operator does?

I am not sure. I got help from an earlier post with the "%".

i want the program to findt the nearest 5. exampe. weight can not be 16.
so how I understood it was. 16 % 5 = 1 (count with your fingers 5 - 10 - 15.. 3 times, how much is left = 1)
and then you say 16(the weight)-(16 % 5) = 15

is that right?

How else could i write it? :)
Reply
#8
(Sep-03-2019, 06:09 PM)d3fi Wrote: is that right?
yes, it was my confusion how you want to calculate weight

for example
def adrenalin():
    weight = int(input("Hvad vejer patienten? ")) #what does the patient weight
    left = weight % 5
    weight -= left
    if left > 2:
        weight += 5
    dosis = min(weight * 0.01, 0.5)
    print(f"Du skal give {dosis:.2f} mg, sv. {dosis:.2f} ml!")
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
#9
def adrenalin():
    weight = int(input("Hvad vejer patienten? ")) #what does the patient weight
    left = weight % 5 
    weight -= left
    if weight >= 50:
        dosis = 0.5
    else:
        weight =  weight + 5 if left > 2 else weight
        dosis = weight * 0.01
    print(f"Du skal give {dosis:.2f} mg, sv. {dosis:.2f} ml!")
this if you want to keep your way of calculating
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
#10
def adrenalin():
    weight = int(input("Hvad vejer patienten? ")) #what does the patient weight
    left = weight % 5 
    weight -= left
    if weight >= 50:
        dosis = 0.5
    else:
        weight =  weight + 5 if left > 2 else weight
        dosis = weight * 0.01
    print(f"Du skal give {dosis:.2f} mg, sv. {dosis:.2f} ml!")
my brain is about to explode. so simple for you, so complex to me.
I understand it until line 9
what does "{....:2f} mean? I understand the {dosis:..} fine. thats the one to be used.
When do you use {....:3f or 4f or etc.}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Referenced before assignment finndude 3 681 Mar-02-2021, 08:11 PM
Last Post: finndude
  exec + subprocess = UnboundLocalError paul18fr 6 757 Feb-04-2021, 06:27 AM
Last Post: Gribouillis
  Assignment of non-existing class variable is accepted - Why? DrZ 6 1,051 Jul-13-2020, 03:53 PM
Last Post: deanhystad
  UnboundLocalError: local variable 'figure_perso' referenced before assignment mederic39 2 748 Jun-11-2020, 12:45 PM
Last Post: Yoriz
  UnBoundLocalError Seaninho 3 843 May-31-2020, 07:22 AM
Last Post: azajali43
  local variable 'marks' referenced before assignment Calli 3 770 May-25-2020, 03:15 PM
Last Post: Calli
  Variable assignment wierdness with Memory eoins 1 725 Mar-08-2020, 10:15 AM
Last Post: scidam
  UnboundLocalError: local variable referenced before assignment svr 1 1,337 Dec-27-2019, 09:08 AM
Last Post: perfringo
  local variable troubles yokaso 4 1,219 Oct-20-2019, 05:25 PM
Last Post: ichabod801
  accessing local variable outside class priyanka08 3 827 Sep-24-2019, 10:00 AM
Last Post: buran

Forum Jump:

User Panel Messages

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