Talha Farrukh Talha Farrukh - 1 month ago 9
Python Question

Comparing the values within a specified range

I am working on a python code and need to compare that whether two values are within the 10% range of each other and I am not getting what code I can implement to handle this.

For e.g. for one item the value of fat is 100 and for the other it is 110. Now I want to check whether both of the values are within the 10% range of each other and if this condition is met I have to print something (I can do it). I just want to know what to write in the if condition of the program.

EDIT: This is what I have currently

if button_id == 'middle':
if mydict['cholesterol'] <= 0.10*mydict1['cholesterol']:
tkinter.messagebox.showinfo('Correct', 'You got it right')
else:
tkinter.messagebox.showinfo('Wrong', 'You got it wrong')


I am stuck in the second IF condition where I have to check whether the values are within the 10% range of each other.

Answer
if value1 + (value1 * 0.1) >= value2 or value1 - (value1 * 0.1) <= value2:
    print("Values are within 10 percent margin")

In other words,

if value1 + 10% of value1 is greater than or equal to value2

or

if value1 - 10% of value1 is lesser than or equal to value2

Then the values are within 10% of each other.

Comments