Hi, it's been a while since I have used Python and I am very rusty. I have a problem with a piece of code and I can't for the life of me figure out what is wrong.

Basically I need a piece of code that prints 'to cold' if the user input is less than 21 and prints 'to hot' if the user input is over 24. However it prints to hot whatever valur I type in.

Any help would be appreciated.

``````var = raw_input("Enter temperature: ")
temperature = raw_input

if temperature < 21:
print "temperature to cold"
elif temperature > 24:
print "temperature to hot"

raw_input()
``````
5
Contributors
6
Replies
39
Views
4 Years
Discussion Span
Last Post by Schol-R-LEA

You should read about what a function, a funciton call, a return value and a variable are.

``````var = raw_input("Enter temperature: ")
temperature = int(var)

if temperature < 21:
print "temperature to cold"
elif temperature > 24:
print "temperature to hot"

#raw_input()
``````
``````coldPoit = 21
while True:
try:
temp = int(raw_input("Enter temperature: "))
if temp <= coldPoit:
print "emperature to cold"
else:
print "temperature to hot"
except ValueError:

temp = int(raw_input("Enter temperature: "))

>>> ================================ RESTART ================================
>>>
Enter temperature: 18
emperature to cold
Enter temperature: 21
emperature to cold
Enter temperature: 23
temperature to hot
Enter temperature: python
Enter temperature: 25
temperature to hot
Enter temperature:
``````

If you use input instead of raw_input it will look for input as an integer as opposed to string. If you do want to use raw_input then use int(temp) < 21 and int(temp) > 24

``````temp = input('Enter temperature: ')

if temp < 21:
print 'Too cold'
elif temp > 24:
print 'Too hot'
``````

Using the `input()` function in Python 2.x is generally considered a bad idea, as it will actually evaluate the statement as if it were a line of Python code. This leaves a large security hole that anyone could exploit - if,instead of just a number, the user were to enter something like;

``````os.chdir('/')
``````

they could play merry hell with the program and/or the operating system. Using `raw_input()` and then casting to `int` is generally the safest way to read an integer value in.

In Python 3.x, the `input()` function has been changed to behave like `raw_input()` did in Python 2.x; i.e., it reads in a string of text without processing it.

``````def temperature():
temp = int(raw_input("Enter your temperature:? "))
if temp <= 21:
print "temperature to cold"
else:
print "temperature to hot"

r = temperature()

>>>
temperature to cold
>>> ================================ RESTART ================================
>>>
temperature to cold
>>> ================================ RESTART ================================
>>>
temperature to hot
``````

Let's try this again, shall we?

``````def temperature():
temp = None
coldPoint = 21
hotPoint = 24

while temp is None:
try:
temp = int(raw_input("Enter temperature: "))
except ValueError:

print "The temperature is",

if temp <= coldPoint:
print "too cold."
elif temp >= hotPoint:
print "too hot."
else:
print "acceptable."

if __name__ == "__main__":
temperature()
``````

Edited by Schol-R-LEA

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.