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()

Recommended Answers

All 6 Replies

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:
        print("Ooops! Enter number Please !")



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
Ooops! Enter number Please !
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()


>>> 
Enter your temperature:? 20
temperature to cold
>>> ================================ RESTART ================================
>>> 
Enter your temperature:? 21
temperature to cold
>>> ================================ RESTART ================================
>>> 
Enter your temperature:? 22
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("Ooops! Enter number Please !")

    print "The temperature is",

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

if __name__ == "__main__":
    temperature()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.