0

I'm literally two days into the learning journey so go easy! I'm trying to put together a basic temperature app and everything runs great without errors except when I input single digits. If I input double digits, it correctly registers the right response, as in: "yes, that's too hot" or "yes, that's too cold" but it seems to recognize anything lower than 10 as being greater than 32 and not lower than 27 thus giving a "too warm" response.`

temperature = range(-30,55)

temperature = input("What is the temperature in Celcius? ")
print("Temperature: " + str(temperature) + " degrees Celcius")

if temperature < str(27):
    print ("Plant is too cold")
if temperature < str(27):
    sum = 27 - int(temperature)
    print("Recommended temperature increase:" + str(sum) + " degrees Celcius")
    print("Remember to keep your plant between 27 and 32 degrees!")
elif temperature > str(32):
    print ("Plant is too warm")
if temperature > str(32):
    sum = int(temperature) - 32
    print("Recommended temperature decrease:" + str(sum) + " degrees Celcius")
    print("Remember to keep your plant between 27 and 32 degrees!")
elif temperature > str(27) and temperature < str(32):
    print ("Plant temperature is nominal")
Blckknght
  • 100,903
  • 11
  • 120
  • 169
JMWrites
  • 1
  • 1
  • You have added the `python` tag, but you have added no code to your question. – quamrana Jun 24 '21 at 16:30
  • 4
    And I'd guess you're comparing _strings_, despite the tag [tag:numbers] - in that case, see https://stackoverflow.com/q/20449427/3001761. But please give a proper [mre]. – jonrsharpe Jun 24 '21 at 16:30
  • You must provide all code as text, not as images of text. – Random Davis Jun 24 '21 at 16:34
  • Please edit your question and post the code as text. There's no easy way to try code in an image ourselves, we can't copy snippets of it into our answers, and it won't show up in future Google searches. See also: [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/q/285551/68587) – John Kugelman Jun 24 '21 at 16:34
  • In lexicographical order, `"Zoo"` comes after `"Aardvark"` despite the former being much shorter. It works the same way for the numeric strings you are comparing. Only their first digits are being compared, which results in a different order than you expect. – Blckknght Jun 24 '21 at 16:42
  • Apologies. Just figured out how to format a code entry here. Need more coffee. – JMWrites Jun 24 '21 at 16:43

2 Answers2

0

It is the difference between comparing strings:

>>> '9'>'27'
True

And integers:

>>> 9>27
False

Comparing different types is an error:

>>> '9'>27
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'

To fix it, convert the string into an int:

>>> int('9')>int('27')
False
dawg
  • 98,345
  • 23
  • 131
  • 206
  • Thanks for that! Really loving this whole "it works most of the time except when it doesn't and I don't know why" feeling. Someone has already told me to give up :') Appreciate the help! – JMWrites Jun 24 '21 at 16:54
  • Python is moderately typed. ie you need to be purposeful of the types of objects you are comparing. Most newer languages (Ruby, Julia, Python, Go) are the same. If you want something that would compare `'9'>27` and figure out to convert the `'9'` to an integer, use Perl or awk -- older languages without the same object typing. – dawg Jun 24 '21 at 17:18
0

You're comparing strings when you should be comparing integers. Python automatically accepts inputs as strings so convert it to an integer like this.

temperature = int(input("What is the temperature in Celcius? "))

Pro tip: it's good practice to verify that your input is a number before you convert it to an integer to avoid errors. You tried to do that here:

temperature = range(-30,55)

But that statement does not set parameters for the temperature variable to only allow numbers between -30 and 55. Instead it just makes temperature equal to a range(-30,55) Use the isdigit() method and conditional statements to verify only the correct input will be received. For this you will need a while loop that asks for an input with specific criteria until one is accepted.

while True:
    temperature = input("What is the temperature in Celcius? ")
    if temperature.isdigit():
        temperature = int(temperature)
        if temperature >= -30 and temperature <= 55:
            break
        else:
            # temperature not in range
    else:
        # temperature is not a digit

Compare the integers like so. Notice how I don't use the str() method during comparison and have no duplicate conditional statements. I also make use of the else statement because all other cases have been covered by the temperature being less than 27 and greater than 32, so the only other option would be between 27 and 32.

if temperature < 27:
    print ("Plant is too cold")
    sum = 27 - temperature
    print("Recommended temperature increase:" + str(sum) + " degrees Celcius")
    print("Remember to keep your plant between 27 and 32 degrees!")
elif temperature > 32:
    print ("Plant is too warm")
    sum = temperature - 32
    print("Recommended temperature decrease:" + str(sum) + " degrees Celcius")
    print("Remember to keep your plant between 27 and 32 degrees!")
else:
    print ("Plant temperature is nominal")
slick
  • 36
  • 5