i am a bit stuck on this, i need to call the first 2 functions for the output in the 3rd one. The user has to should be able to continue until they want to stop converting and they should be asked on which way the conversion is to be performed?

def fahrenheit2Celsius(fahrenheit):
    return (fahrenheit - 32) * 5.0 / 9.0
   
def celsius2Fahrenheit(celsius):
    return 9.0 / 5.0 * celsius + 32

def fahrenheit2celsius():
    temeperature= input("please enter the temperature to convert: ")
    fahrenheitorcelsius=input("1=farenheit/2=celsius")
    if fahrenheitorcelsius==1:
        fahrenheit2Celsius(fahrenheit)
    elif fahrenheitorcelsius==2
          celsius2Fahrenheit(celsius)
Member Avatar for masterofpuppets

well, the code is probably giving an error message, saying that 'fahrenheit' or 'celsius' is not defined and this is correct - you haven't defined them anywhere. They are just parameters in the two functions. That means you can't use them anywhere else but inside the function. So you should call the functions with 'temperature' as an argument like this:

def fahrenheit2Celsius( fahrenheit ):
    return ( fahrenheit - 32 ) * 5.0 / 9.0
 
def celsius2Fahrenheit( celsius ):
    return 9.0 / 5.0 * celsius + 32
 
def fahrenheit2celsius():
    temperature = input( "Please enter the temperature to convert: " )
    fahrenheitorcelsius = input( "1 = farenheit / 2 = celsius: " )

    if fahrenheitorcelsius == 1:
        print temperature, "in Fahrenheit is", fahrenheit2Celsius( temperature ), "Celsius"
    elif fahrenheitorcelsius == 2:
        print temperature, "in Celsius is", celsius2Fahrenheit( temperature ), "Fahrenheit"

fahrenheit2celsius()

suppose that the user enters 43 and also enters 2 after this. Here's what happens - the if is skipped because fahrenheitorcelsius is 2; then the program prints the message and calls the function celsius2Fahrenheit with temperature, which is 43, as an argument, so inside the function 'celsius' becomes equal to 43 so the function performs the computation 9.0 / 5.0 * 43 + 32 and returns the result.

hope this helps :)

commented: Very good explanation! +5

Ya basically you need to call the local variable temperature not the parameter from the original function. And if you want to keep going through conversions as the user states, you can just use a for loop and and if and else statement. I used:

def main():
    fahrenheit2celsius()
    for i in range(10000):
        cont = raw_input('Would you like to continue converting? (yes/no): ')
        if cont == 'yes' or cont == 'Yes':
           fahrenheit2celsius()
        else:
            break

that should keep it going until the user specifies to stop. If that's what you meant by keep going...

how come when i run this code the loop does not work?

def fahrenheit2Celsius( fahrenheit ):
    return ( fahrenheit - 32 ) * 5.0 / 9.0
 
def celsius2Fahrenheit( celsius ):
    return 9.0 / 5.0 * celsius + 32
 
def fahrenheit2celsius():
    temperature = input( "Please enter the temperature to convert: " )
    fahrenheitorcelsius = input( "1 = farenheit / 2 = celsius: " )

    if fahrenheitorcelsius == 1:
        print temperature, "in Fahrenheit is", fahrenheit2Celsius( temperature ), "Celsius"
    elif fahrenheitorcelsius == 2:
        print temperature, "in Celsius is", celsius2Fahrenheit( temperature ), "Fahrenheit"

def main():
    fahrenheit2celsius()
    for i in range(10000):
        cont = raw_input('Would you like to continue converting? (yes/no): ')
        if cont == 'yes' or cont == 'Yes':
           fahrenheit2celsius()
        else:
            break
fahrenheit2celsius()
Member Avatar for masterofpuppets

Because at the end you need to call main instead of fahrenheit2celsius :)
like this:

def main():
    fahrenheit2celsius()
    for i in range(10000):
        cont = raw_input('Would you like to continue converting? (yes/no): ')
        if cont == 'yes' or cont == 'Yes':
           fahrenheit2celsius()
        else:
            break

main()

The function main() can be simplified ...

def main():
    while True:
        fahrenheit2celsius()
        cont = raw_input('Continue converting? (yes/no): ')
        if 'n' in cont.lower():
            break

while True forms an endless loop and the n in no or NO will break out of the loop.

This will give your program a certain eloquence:

# convert temperatures using Python2 or Python3

def f2c( fahrenheit ):
    """convert Fahrenheit to Celsius"""
    return ( fahrenheit - 32 ) * 5.0 / 9.0

def c2f( celsius ):
    """convert Celsius to Fahrenheit"""
    return 9.0 / 5.0 * celsius + 32

def main(info):
    print( info )
    while True:
        t = str(input( "Enter the temperature: " )).lower()
        if 'q' in t:
            break
        if 'f' in t:
            # remove last char, convert to number
            num = float(t[:-1])
            celsius = f2c(num)
            print( "%0.1f F = %0.1f C" % (num, celsius) )
        elif 'c' in t:
            # remove last char, convert to number
            num = float(t[:-1])
            fahrenheit = c2f(num)
            print( "%0.1f C = %0.1f F" % (num, fahrenheit) )


info = """\
Enter the temperature with the unit of measurement,
for instance ...
100F will give the temperature in Celsius
100C will give the temperature in Fahrenheit
(enter just q to quit the program)
"""

# program starts here
main(info)

Not totally fool proof, but what is?

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.