Stefan
Stefan

Reputation: 37

set up if statement for temperature conversions?

The given function is:

def Temp_Conv(Temp_in, case):

Where Temp_in is an integer, and case is a string. Case can be inputted as 'C2F' (celsius to fahrenheit) or 'F2C' (fahrenheit to celsius). I only need to convert celsius to fahrenheit, so when case=='C2F', then I will apply the conversion formula. However, if case=='F2C' then the return must be 'Wrong case value'. This is how I set up my program:

def Temp_Conv(Temp_in,case):
    if case=='C2F':
        return ((Temp_in*(9/5)) + 32)
    if case=='F2C'
        return 'Wrong case value'

Is this the correct way to do this?

Upvotes: 1

Views: 385

Answers (1)

Prab
Prab

Reputation: 504

To test if your funciton works.

def Temp_Conv(Temp_in,case):
    if case=='C2F':
        return ((Temp_in*(9/5)) + 32)
    if case=='F2C':
        return 'Wrong case value'

case = input("please enter a case: ")
temp = 30
print(Temp_Conv(temp, case))

This should run the function to see if it works or not.

Upvotes: 2

Related Questions