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
  It's saying my global variable is a local variable Radical 5 1,098 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  local varible referenced before assignment SC4R 6 1,465 Jan-10-2023, 10:58 PM
Last Post: snippsat
  How does UnboundLocalError work? deanhystad 3 1,633 Feb-25-2022, 01:21 AM
Last Post: bowlofred
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 1,859 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  Referenced before assignment finndude 3 3,223 Mar-02-2021, 08:11 PM
Last Post: finndude
  exec + subprocess = UnboundLocalError paul18fr 6 3,444 Feb-04-2021, 06:27 AM
Last Post: Gribouillis
  ReferenceError: weakly-referenced object no longer exists MrBitPythoner 17 11,267 Dec-14-2020, 07:34 PM
Last Post: buran
  Assignment of non-existing class variable is accepted - Why? DrZ 6 4,188 Jul-13-2020, 03:53 PM
Last Post: deanhystad
  UnboundLocalError: local variable 'figure_perso' referenced before assignment mederic39 2 2,229 Jun-11-2020, 12:45 PM
Last Post: Yoriz
  UnBoundLocalError Seaninho 3 2,624 May-31-2020, 07:22 AM
Last Post: azajali43

Forum Jump:

User Panel Messages

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