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

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 masterofpuppets

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.