Friday, July 31, 2009

HELP programming a python problem?

The problem is to write afunction body where unit is a temperature unit, either 'F' or 'C', and T is a temperature value (of type float) in that unit. The function should return the temperature (of type float) in the other unit.





This is the code I wrote so far, but I'm facing some syntax problems.





def temp(T, %26lt;unit%26gt;):





if


%26lt;unit%26gt; == ['F']


T_F=(5.0/9.0)*float(T)-32.0


T=T_F


return T_F,'C'





else


%26lt;unit%26gt; == ['C']


T_C= (9.0/5.0)*float(T)+32.0


T=T_C


return T_C,'F'














what is wrong with my code?

HELP programming a python problem?
no clue... sorry
Reply:Well if you have a decent compiler (not sure what language you are using, whatever it is, havnt really seen it much) then it would really help if we could see what error it is returning to you. Sorry if this wasnt much help
Reply:first, you have to indent. Indent everything that's in a function under the function declarations:


Next, everything in an if or else statement also has to be tabbed:





Now, you have some syntax errors. First, after saying 'if' or


'else', you need a colon.


Also, when you are doing math, it sometimes helps to put spaces between the different statements.


When you are setting a value equal to something, use '='; '==' is used for comparing to values.


The working code should be this:





def temp(T, %26lt;unit%26gt;):





if:


%26lt;unit%26gt; = ['F']


T_F = (5.0 / 9.0) * float(T) - 32.0


T = T_F


return T_F,'C'





else:


%26lt;unit%26gt; = ['C']


T_C = (9.0 / 5.0) * float(T) + 32.0


T = T_C


return T_C,'F'





That should work. If not, e-mail me the code and the error statement, and I'll see what I can do.


P.S.--Remember to include the indentions--they don't show up for some reason.


No comments:

Post a Comment