making for practice a temp converter. i got the basic program to convert C to F. heres that part:

C = input("what temperature (celius) do you need converted? ")
print "%s degrees celsius is equal to %s degrees Farenheit" % (C, 32+(212-32) / 100.0 * C

now im trying to add an input to have the user choose if they want to convert from C to F or from F to C. keeps saying "name 'c' is not defined" cant seem to fig it out. heres the code:

sel = input("select C for c to f, or F for f to c ? ")
print "%s degrees farenheit is equal to %s degrees celcius" % ((F-32) * 100.0 / (212-32))
F = input("what temp (farenheit) do you need converted? ")
C = input("what temperature (celius) do you need converted? ")
print "%s degrees celsius is equal to %s degrees Farenheit" % (C, 32+(212-32) / 100.0 * C)

any help would be much appreciated as i am teaching myself and this is my 1st and only language so far so its tough. ty in advance. :)

Recommended Answers

All 10 Replies

It looks like you're using a Python version that is not 3.X ... so in that case you should be using raw_input() instead of input().

raw_input stores the user's input as a string, which is the way input() works in Python 3.0 and up

i tried switching to "raw_input", but still says "name "c" or "f"..whatever user enters..is not defined. i am using 2.6.4 atm. :)

That's because you're trying to print the result before you even ask the user for the number that they want to input.

ok, ty..i moved my print statements to the bottom of the code..now it doesnt respond to user input it goes on to next line and asks" what farenheit temp you need converted" even if u ask for celcius to be converted.. how do i make it respond to user input at first line?

im trying to add an "if" statement to specify a formula to be used for conversion. not getting it right though. :)

ok, here's were im stuck at now..i input "c" or "f" and hit enter and it does nothing at all...lol:

sel = raw_input("select C for c to f, or F for f to c ? ")
if raw_input("C"):
    print "%s degrees celsius is equal to %s degrees Farenheit" % (c, 32+(212-32) / 100.0 * c)
if raw_input("F"):
    print "%s degrees farenheit is equal to %s degrees celcius" % (f,(f-32) * 100.0 / (212-32))
    
f = raw_input("what temp (farenheit) do you need converted? ")
c = raw_input("what temperature (celius) do you need converted? ")

Your code is awaiting your next raw_input at the "C" prompt.... You should be comparing the value of sel . Also, you've done the exact same thing as before. You can't print the value before you get it from the user or your code will complain about that object not existing. You need to modify your code like so:

sel = raw_input("select C for c to f, or F for f to c ? ")
if sel == 'C':
    c = raw_input("what temperature (celius) do you need converted? ")
    print "%s degrees celsius is equal to %s degrees Farenheit" % (c, 32+(212-32) / 100.0 * c)
elif sel == 'F':
    f = raw_input("what temp (farenheit) do you need converted? ")
    print "%s degrees farenheit is equal to %s degrees celcius" % (f,(f-32) * 100.0 / (212-32))

aww man i was almost ther too when i checked on here for a reply, ty so so much for ur help, i hope i wasnt too annoying..lol.. like i said this is my 1st and only language so far. but ty very much, awesome. :) it always seems so "common sense" after these things get solved..lol :)

#!/usr/ben/python
#Temp conversion 


sel = raw_input("select C for celcius to farenheit, or F for farenheit to celcius ? ")
if sel=="C":
    c = input("what temperature (celius) do you need converted? ")
    print "%s degrees celsius is equal to %s degrees Farenheit" % (c,32+(212-32) / 100 * c)
elif sel=="F":
    f = input("what temp (farenheit) do you need converted? ")
    print "%s degrees farenheit is equal to %s degrees celcius" % (f,(f-32) * 100 / (212-32))

how can i make it loop back to beginning to continue running after it does a conversion for me??

This is a very simple way:

#!/usr/ben/python
#Temp conversion 

repeat = 'y'
while repeat == 'y':
    sel = raw_input("select C for celcius to farenheit, or F for farenheit to celcius ? ")
    if sel=="C":
        c = input("what temperature (celius) do you need converted? ")
        print "%s degrees celsius is equal to %s degrees Farenheit" % (c,32+(212-32) / 100 * c)
    elif sel=="F":
        f = input("what temp (farenheit) do you need converted? ")
        print "%s degrees farenheit is equal to %s degrees celcius" % (f,(f-32) * 100 / (212-32))
    repeat = raw_input("Would you like to repeat? (y/n)")

ur pretty slick..ty vry much 4 ur help. :)

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.