Python Experiment - NEED SOME GUIDANCE


<Below this line, add a link to the EXACT exercise that you are stuck at. The query string (? and beyond) may be truncated.>
Trying to write a Conditional Logic Program… Having an issue with the Operators.
When I run it in the module, the Output states “Low blood sugar.” when it should state “Slightly high blood sugar.

The value is 102.

Any examples to help me learn what I’m doing wrong here would be greatly appreciated. Thanks!

<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>


#!/usr/bin/env python

“”" Test “”"
import json

def test_blood_glucose():

blood_glucose = ‘{“value”: “102”, “unit”: “mg/dL”}’

parsed_json = json.loads(blood_glucose)


glucose_reading = (parsed_json[‘value’])

if (glucose_reading >= ‘1.00’ and glucose_reading <= ‘68.00’):
print “Low blood sugar.”

elif (glucose_reading >= ‘69.00’ and glucose_reading <= ‘98.00’):
print “Normal blood sugar.”

elif (glucose_reading >= ‘99.00’ and glucose_reading <= ‘125.00’):
print “Slightly high blood sugar.”

elif (glucose_reading >= ‘126.00’ and glucose_reading <= ‘198.00’):
print “High blood sugar.”

elif (glucose_reading >= ‘199.00’ and glucose_reading <= ‘918.00’):
print “Very high blood sugar.”

print “Nothing to say.”


<do not remove the three backticks above>

First of all: it’s awesome that your trying your hand at real world examples! Keep up the good work.

Why does it happen?

Your problem is that you’re comparing in a wrong way. If you compare strings in python, which you are doing, you are comparing them lexicographically (or ‘dictionary ordering’). Let’s see what it does with "102" >= "1.00":

1     0    2
1     .    0    0
_     _    _

=     >  

'0' > '.', what does this even mean??? It doesn’t actually compare the values, but the ascii codes. The actual comparison that happens is 48 > 46.

We can do the same thing for '102' <= '68.00':

1     0    2
6     8    .    0
_     _    _


Since '1' < '6', we can already stop. Now both the conditions are true, so that’s why it prints "Low blood sugar.".

How to fix it?

The simple way to fix it, is to stop comparing strings. You could just convert the glucose_reading to an int immediately. You can do this like this: glucose_reading = int(parsed_json['value']).

This should do it!
Don’t forget to then remove the '' from your comparisons as well.

1 Like

i don’t know but i will try your code, the value “102” it’s an integer so glucose_reading = (parsed_json['value']) return an integer ?? test it and reply to me
and can we test an experssion by < or > or === if it’s a caracter( string ) ??

If you look at your json string:

{"value": "102", "unit": "mg/dL"}

you can see that "value" has a value of "102", which is a string.

This is why you have to convert it. You could also change the json if you want, but I don’t know if you might be working with an API or something to get that json.

You can, but not with the built-in >, < and ==. You could write your own comparison function, where it first checks the lengths, but this is way too tedious. It’s way better to just convert to an int.

1 Like

Hi Nicoe,

Your response was very helpful and enlightening to me. I figured the comparators were off, I just didn’t know how to use the int() function. Sooooo Helpful!

I learned something new today :slight_smile:

Thank you

1 Like