def main1(f):
    return (f - 32) * 5.0 / 9.0

def main2(c):
    return 9.0 / 5.0 * c + 32

the two function above convert fahrenheit and celsius

I have to write a third function which calls the two functions above and allows the user to input to convert between tempertures repeatedly, until the user wants to stop. the user chooses in which way the conversion iis performed. I am not sure how to start my code. Please help


Please do not flag to quote because I use a handheld device to view the forum.

Thanks

Dani AI

Generated

A concise, robust approach complements the fixes already posted by and (the original problems were mainly indentation and wrong variable names). The next steps are small improvements that make the converter friendlier and safer: accept decimal temperatures, normalize and validate user input, tolerate lower/upper case and stray spaces, format results, and provide clear error messages. An editor that auto-indents (as suggested) will prevent the common block/indent errors seen earlier.

A compact pattern is to separate conversion logic from the interaction, validate the numeric input with exception handling, and normalize the choice with .strip().upper(). The snippet below demonstrates that pattern (keeps conversion functions separate, supports decimals, and loops until the user quits). For Python 3, replace raw_input with input and use print() calls.

def to_celsius(f):
    return (f - 32) * 5.0 / 9.0

def to_fahrenheit(c):
    return c * 9.0 / 5.0 + 32

def run_converter():
    while True:
        s = raw_input("Enter temperature (or Q to quit): ").strip()
        if s.upper() == "Q":
            break
        try:
            temp = float(s)
        except ValueError:
            print "Invalid number; try again."
            continue
        choice = raw_input("Convert to (C/F): ").strip().upper()
        if choice == "C":
            print "{:.2f} C".format(to_celsius(temp))
        elif choice == "F":
            print "{:.2f} F".format(to_fahrenheit(temp))
        else:
            print "Enter C or F."

if __name__ == "__main__":
    run_converter()

Troubleshooting notes: prefer float() over int() to avoid truncation; always re-prompt inside the loop to avoid infinite loops; test edge cases (negative temps, decimals). For extensions, consider parsing unit-suffixed entries like 32F or offering a menu to convert the same value multiple ways.

Recommended Answers

All 7 Replies

So you're going to ask the user to put in a starting temperature

start_temp = int( raw_input('Enter a temperature: ') )

Then you need to give the user some kind of menu or options

choice = raw_input('Enter C for celcius, F for farenheit, or Q for quit:')

Then you need to enter a never-ending loop that will end when the user presses Q.

while choice != 'Q':
    if choice = 'F':
        print main1(temperature)
    if choice = 'C':
        print main2(temperature)
    choice = raw_input('Enter C for celcius, F for farenheit, or Q for quit:')

Am I understanding your question correctly?

So you're going to ask the user to put in a starting temperature

start_temp = int( raw_input('Enter a temperature: ') )

Then you need to give the user some kind of menu or options

choice = raw_input('Enter C for celcius, F for farenheit, or Q for quit:')

Then you need to enter a never-ending loop that will end when the user presses Q.

while choice != 'Q':
    if choice = 'F':
        print main1(temperature)
    if choice = 'C':
        print main2(temperature)
    choice = raw_input('Enter C for celcius, F for farenheit, or Q for quit:')

Am I understanding your question correctly?

Yes, I will get back to you if I have futhur problem.

Thanks for your help

def main1(f):    
return (f - 32) * 5.0 / 9.0

def main2(c):   
return 9.0 / 5.0 * c + 32

def temperture():
start = int(raw_input("Please enter a temperture: "))
option = raw_input('Enter C for celcius, F for farenheit, or Q for quit:')
while option != "Q":
        if option == "F":
            print main1(f)
    if option == "C":
        print  main2(c):

This is my function at the moment, it does work properly, but there isn' a error. Please help!

Member Avatar for Member #562630

hi,
a couple of errors in your functions:
first, try to fix your indentation in the while loop, i.e. for the first if statement
second, the error occurs because you are calling the functions with variables c and f which are not defined in your main function. You should be calling the functions with the variable start instead. c and f are just parameters in the functions :)
third, you need to get the input from the user again at the end of the while loop because otherwise you go into an infinite loop.

it should be something like this:

def main1( f ):    
    return (f - 32) * 5.0 / 9.0
 
def main2( c ):   
    return 9.0 / 5.0 * c + 32
 
def temperature():
    start = int(raw_input("Please enter a temperture: "))
    option = raw_input('Enter C for celcius, F for farenheit, or Q for quit:')
    while option != "Q":
        if option == "F":
            print main1( start )
        if option == "C":
            print  main2( start )

        start = int(raw_input("Please enter a temperture: "))
        option = raw_input('Enter C for celcius, F for farenheit, or Q for quit:')

temperature()

hope this helps :)

thanks man, so my main problem is indentation.

thanks man, so my main problem is indentation.

Python relies on indentations for it's code blocks. Other computer languages use braces, begin, end and so on. So yes, if you want to code in Python, then poorly done indentations can be a real problem! Remember, a code block usually starts after a a code line ending with a : mark.

Use a good Python editor for your coding and it will auto-indent and flag you down if you miss it. It will also show indentation markers.

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.