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. :)

Dani AI

Generated

Quick diagnosis and follow-up to the helpful fixes already in the thread: the NameError happened because Python 2's input() evaluates what is typed (so entering C without quotes is treated as a name). was right to move numeric input until after the choice and to use raw_input() in Python 2. Going further, a few simple robustness items make the script easier to maintain and less error-prone.

  • Read the choice first, normalize it (.strip().lower()), then ask for the numeric value.
  • Convert number text to float() and catch ValueError so bad input doesn't crash the script.
  • Use float constants (for example 9.0/5) or from __future__ import division to avoid integer-division surprises in Python 2.
  • Put conversion logic into small functions and add a repeat loop so the flow is clear.

Example (cross-Python-2/3 friendly, and intentionally different from earlier snippets):

try:
    input_fn = raw_input
except NameError:
    input_fn = input

def c_to_f(c): return c * 9.0/5 + 32
def f_to_c(f): return (f - 32) * 5.0/9

while True:
    sel = input_fn("Convert (C)elsius or (F)ahrenheit? ").strip().lower()
    if sel not in ('c', 'f'):
        print("Please enter C or F.")
        continue
    while True:
        try:
            t = float(input_fn("Temperature: "))
            print("{:.2f} -> {:.2f}".format(t, c_to_f(t) if sel=='c' else f_to_c(t)))
            break
        except ValueError:
            print("Enter a numeric temperature.")
    if input_fn("Repeat? (y/n) ").strip().lower() != 'y':
        break

Troubleshooting note: if NameError still appears, confirm the script is run under the expected interpreter (Python 2 vs 3) and avoid using input() in Python 2, since it evaluates input as code. The progress from shows the right learning steps — selection first, numeric input second, then output.

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.