It says I haven't defined the variable even though the code that does that is literally right above it, the error message appears:
"Traceback (most recent call last): File "main.py", line 55, in if kategoribmi == "underweight": NameError: name 'kategoribmi' is not defined"
if kelas == "('15', 'P')":
if bmi > 28.2:
kategoribmi = "obese"
elif bmi >= 23.6 < 28.2:
kategoribmi = "overweight"
elif bmi <= 15.8:
kategoribmi = "underweight"
else:
kategoribmi = "ideal"
if kategoribmi == "underweight":
underweight()
elif kategoribmi == "overweight":
overweight()
elif kategoribmi == "obese":
obese()
else:
ideal()
I'm really unsure what to do here, I've tried looking up how to fix this but I'm still unclear.
CodePudding user response:
Your code is a bit incomplete, perhaps it is inside a function? Is there a previous condition that has to evaluate to True
? For better help you should provide more code. Are you sure your code can evaluate to True
for the condition if kelas == "('15', 'P')"
every time you run it?. On a side note, you are leaving out the value 28.2
Hope it helps
CodePudding user response:
If the variable kelas
is not equal to the string "('15', 'P')"
, for example if it is a tuple ('15', 'P')
instead of a string, or if it is any other value like "anything"
, the code jumps right to the line if kategoribmi == "underweight"
, not executing the ones inside the if statement.
I don't know what you are trying to do, but one of the possible solutions is to increase the bottom part's indentation so that it is inside the if statement above: (if you intend it to only be run when kelas=="('15', 'P')"
.
if kelas == "('15', 'P')":
if bmi > 28.2:
kategoribmi = "obese"
elif bmi >= 23.6 < 28.2:
kategoribmi = "overweight"
elif bmi <= 15.8:
kategoribmi = "underweight"
else:
kategoribmi = "ideal"
if kategoribmi == "underweight":
underweight()
elif kategoribmi == "overweight":
overweight()
elif kategoribmi == "obese":
obese()
else:
ideal()
Another solution is to change the kelas=="('15', 'P')"
to kelas==('15', 'P')
. Maybe you were trying to compare the kelas
as a tuple. Anyways, without the complete minimum reproducible code, we cannot provide much for you.
CodePudding user response:
This happens because when the compiler checks your code, it cannot be sure whether the execution will reach kategoribmi = "something"
. If the if
statement where you assign a value to kategoribmi
will not be true at execution, the variable will not be assigned. And whether this will be the case or not, cannot be known at compile time.
The simple solution to this is to assign a default value to kategoribmi
before the first set of if
statements, like so:
kategoribmi = ""
if kelas == "('15', 'P')":
if bmi > 28.2:
kategoribmi = "obese"
elif bmi >= 23.6 < 28.2:
kategoribmi = "overweight"
elif bmi <= 15.8:
kategoribmi = "underweight"
else:
kategoribmi = "ideal"
if kategoribmi == "underweight":
underweight()
elif kategoribmi == "overweight":
overweight()
elif kategoribmi == "obese":
obese()
else:
ideal()