UnboundLocalError: local variable ' ' referenced before assignment - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: UnboundLocalError: local variable ' ' referenced before assignment (/thread-20864.html) Pages:
1
2
|
UnboundLocalError: local variable ' ' referenced before assignment - d3fi - Sep-03-2019 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() RE: UnboundLocalError: local variable ' ' referenced before assignment - buran - Sep-03-2019 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. RE: UnboundLocalError: local variable ' ' referenced before assignment - d3fi - Sep-03-2019 (Sep-03-2019, 03:28 PM)buran Wrote: show full traceback in error tags 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!") RE: UnboundLocalError: local variable ' ' referenced before assignment - buran - Sep-03-2019 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) RE: UnboundLocalError: local variable ' ' referenced before assignment - d3fi - Sep-03-2019 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 RE: UnboundLocalError: local variable ' ' referenced before assignment - buran - Sep-03-2019 one thing I think is wrong is left = weight % 5 Do you know what modulo operator does? RE: UnboundLocalError: local variable ' ' referenced before assignment - d3fi - Sep-03-2019 (Sep-03-2019, 05:51 PM)buran Wrote: one thing I think is wrong is 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? :) RE: UnboundLocalError: local variable ' ' referenced before assignment - buran - Sep-03-2019 (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!") RE: UnboundLocalError: local variable ' ' referenced before assignment - buran - Sep-03-2019 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 RE: UnboundLocalError: local variable ' ' referenced before assignment - d3fi - Sep-03-2019 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.} |