How to write if statements to print various strings

I am writing a function that checks if two numbers in a list are within a certain range and then print various statements if one number is in the range or if both numbers are in the range. My problem is that the right statements aren't being printed under the right conditions despite the statements looking fine.

def qualitative_reading(input_list):
  humidity = 0
  temp = 0
  humid_round= round(input_list[0])
  temp_round= round(input_list[1])

  if humid_round in range(39, 61, 1):
    humidity = 1
  else:
    humidity = 0

  if temp_round in range(67, 75, 1):
    temp = 1
  else:
    temp = 0

  if humidity + temp ==2:
    print('The room is comfortable')
  elif temp == 1 & humidity == 0:
    print("Ideal conditions for bacteria and virus")
    print('The room is not comfortable to be in')
  elif temp == 0 & humidity == 1:
    print('The room is not comfortable to be in')
    print('Energy being wasted')
  elif humidity + temp ==0:
    print('The room is not comfortable to be in')

Sample output using similar code Sample output using similar code

Upvotes: 0

Views: 63

Answers (1)

Jhanzaib Humayun
Jhanzaib Humayun

Reputation: 1183

You can try something like this instead

def qualitative_reading(input_list):
  humidity = False
  temp = False
  humid_round= input_list[0]
  temp_round= input_list[1]

  humidity = 39 <= humid_round < 61
  temp = 67 <= temp_round < 75

  if humidity and temp:
    print('The room is comfortable')
  elif temp and not humidity:
    print("Ideal conditions for bacteria and virus")
    print('The room is not comfortable to be in')
  elif not temp and humidity:
    print('The room is not comfortable to be in')
    print('Energy being wasted')
  elif not humidity and not temp:
    print('The room is not comfortable to be in')

I suspect that the problem lies with the fact that you are using & but python uses the keyword and.

Some of the other things I noticed were:

The use of range is not very applicable here so you can use an if statement instead.

humidity and temp seem to only have two values, so they can be turned into booleans.

Upvotes: 1

Related Questions