Use of conditional statements to classify output value

I have written a code in Python which outputs a single value and I want to classify that single output value depending on different conditions and ranges, which are:

enter image description here

I have tried the following but no output was printed:

if (Vk = 0):        st = 'Ideal Homogenous'  if (0 < Vk < 0.25):        st = 'Slightly Heterogenous'  if (0.25 < Vk < 0.50):        st = ' Heterogenous'  if (0.50 < Vk < 0.75):        st = 'Very Heterogenous'  if (0.75 < Vk < 1):        st = 'Extremely Heterogenous'  if (Vk = 1):        st = 'Perfectly Heterogenous' 
Add Comment
2 Answer(s)
  1. use == and not = for comperision inside if statements.

if vk == 0:

  1. print the output using:

print(st)

  1. notice for the value 0.25, 0.5, and 0.75 you will not get the desired output. you need to use >= or <= at least for one on the condition contain one of them.

  2. it is better to use elif and else statements. this way the code will not check all cases all the time. when it will find the right answer it will skip all the rest.

Answered on July 16, 2020.
Add Comment

You need to use print() to display the output in your console. = is used for namespace assignment, == is used to make comparison.

if (Vk == 0):        st = 'Ideal Homogenous'  elif (0 < Vk < 0.25):        st = 'Slightly Heterogenous'  elif (0.25 <= Vk < 0.50):        st = ' Heterogenous'  elif (0.50 <= Vk < 0.75):        st = 'Very Heterogenous'  elif (0.75 <= Vk < 1):        st = 'Extremely Heterogenous'  elif (Vk == 1):        st = 'Perfectly Heterogenous'  print(st) 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.