1

I am trying to write a python function that converts between celsius and farenheit and subsequently a program that first prompts for the temp scale (c or f) and then the temp value and then converts to the other. What I have so far:

def convert_temp( source_temp, scale):
    if scale == 'c':
        return(tmp-32.0)*(5.0/9.0)
    elif scale == 'f':
        return(tmp*(9.0/5/0))+32

source_temp = int(input)'Key in temp:'))
scale = input('(c) or (f)?')
y = conv(source_temp,scale)
print(tmp, 'in ',scale,"='s",y)

however, when I try to run the program, I receive a lot of traceback and syntax errors. What am I doing wrong??

movietime
  • 101
  • 1
  • 10

5 Answers5

2

replace this:

9.0/5/0        # you will get ZeroDivisionError

to:

9.0/5.0

replace this:

source_temp = int(input)'Key in temp:')) # there should be opening bracket after input

to:

source_temp = int(input('Key in temp:'))

replace this:

y = conv(source_temp,scale)

to:

y = conv_temp(source_temp,scale)

make changes to ur print statement:

print(source_tmp, 'in ',scale,"='s",y)       # here tmp was not defined, its source_tmp
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
2

There's a number of problems in your code.

def convert_temp( source_temp, scale):
    if scale == 'c':
        return(tmp-32.0)*(5.0/9.0)
    elif scale == 'f':
        return(tmp*(9.0/5/0))+32

First, tmp is undefined in this scope. Your parameter is named source_temp, not tmp. Changing the function definition will fix that error. Additionally, you've made a typo in one of your expressions and replaced a dot with a slash. This function will work properly:

def convert_temp( tmp, scale):
    if scale == 'c':
        return(tmp-32.0)*(5.0/9.0)
    elif scale == 'f':
        return(tmp*(9.0/5.0))+32

Next, you've made a few syntax errors in the body of your program:

source_temp = int(input)'Key in temp:'))

You have a mismatching parentheses in this line. It should be

source_temp = int(input('Key in temp:'))

Further down:

y = conv(source_temp,scale)

conv() is not a function. Instead, you should be using the convert_temp() function you defined

y = convert_temp(source_temp,scale)

Finally,

print(tmp, 'in ',scale,"='s",y)

tmp is now not defined. Use the source_temp variable that you have defined, as such:

print(source_temp, ' in ',scale," ='s ",y)
David Reeve
  • 863
  • 9
  • 16
  • Thank you this is great! I appreciate the help, my only issue now is I am getting a File "" line 6, invalid syntax pointing to the tmp; I am running python 3.4.1 – movietime Dec 07 '14 at 14:18
  • Could you create a [pastebin](http://pastebin.com/) of your current code? I'm not receiving an error, though I am running 3.4.2 – David Reeve Dec 07 '14 at 17:21
  • def convert_temp( tmp, scale): if scale == 'c': return(tmp-32.0)*(5.0/9.0) elif scale == 'f': return(tmp*(9.0/5.0))+32 tmp = int(input('Key in temp:')) scale = input('(c) or (f)?') y = convert_temp( temp,scale) print(tmp, 'in ',scale,"='s",y); – movietime Dec 07 '14 at 22:23
  • `y = convert_temp(tmp,scale)` for one – David Reeve Dec 07 '14 at 22:42
  • Yeah, even though I have fixed that, I still get error messages that the name 'tmp' is not defined – movietime Dec 08 '14 at 00:15
  • I'm not receiving that error using the code you've given. – David Reeve Dec 08 '14 at 18:22
0

Unbalanced parentheses in this statement are at least part of the problem:

source_temp = int(input)'Key in temp:'))

Try this instead:

source_temp = int(input('Key in temp:'))

Also: conv() is not the same as convert_temp(), raw_input() instead of input(), division-by-zero, etc.

Brian Cain
  • 14,403
  • 3
  • 50
  • 88
0

Others responses are excellent as to what is wrong syntactically with the code, but might I also recommend having only one return statement. Its generally considered bad practice in most programming languages to have more than one return. Also doing it in the following manner prevents misbehavior during running, ie what would happen if you passed a character besides 'c' or 'f'. This is a valid use case especially since the value being passed comes from user input, and there are no prior checks for illegal values.

def convert_temp(source_temp, scale):
    final_temp = 0
    if scale == 'c':
        final_temp = (source_temp - 32.0) * (5.0 / 9.0)
    elif scale == 'f':
        final_temp = (source_temp * (9.0 / 5.0)) + 32
    else:
        raise Exception('Bad Temperature Scale')
    return final_temp

temp_input = int(input('Key in temp:'))
scale_input = input('(c) or (f)?')
y = convert_temp(temp_input, scale_input)
print(tmp, 'in ', scale,"='s", y)

Based on the mistakes made it makes me wonder if further explanation is needed into the actual mechanisms at work here. Since you originally had a mismatch on the function call as well as on the parameters. I suspect you understand what variables are: symbols which refer to a value. source_temp and scale in your convert_temp function are variables local to the function, they do not exist outside the function but they do inside. This concept is called "scope". When the function is called these local variables obtain the value of the parameters in your call:

<return_variable> = convert_temp(<temp_arg>, <scale_arg>).

That leads to my next point: since the convert_temp function is global it is perhaps not best to use the same variable names for your user input as the arguments defined in your function. While not necessarily wrong, its more precaution then anything.

As for the function name itself convert_temp, this is the name with which you call your function. Similar to how variables are references to values, functions are sort of references to behavior. Hence when you call the function you need to use the same symbol with which you defined it.

iceflow19
  • 752
  • 4
  • 12
  • I don't really accept that more than one `return` statement would be a problem here. The bigger problem is what to do if `scale` is not `c` or `f` (which should be validated on input). Instead of returning `0`, `convert_temp()` should raise an Exception in this case. – xnx Dec 06 '14 at 22:54
  • It is not a "problem", as much as what is "good practice". Good point, I'll update my example to throw an exception. I was hesitant to include that as it might be a tad beyond the OP, gauging from the type of mistakes made. – iceflow19 Dec 06 '14 at 23:00
  • 1
    I'd say that your opinion about the number of return statements is invalid. See http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement – Peter M Dec 06 '14 at 23:22
0

Try or adapt this more general function:

C, F, K, R = c, f, k, r = ('C', 'F', 'K', 'R')
SCALES = {C: (0.0, 100.0), F: (32.0, 212.0), K: (273.15, 373.15), R: (491.67, 671.67)}

def convert(temperature, to_scale):
    value, from_scale = temperature
    from_scale_freeze, from_scale_boil = SCALES[from_scale]
    to_scale_freeze, to_scale_boil = SCALES[to_scale]
    from_scale_divisions = from_scale_boil - from_scale_freeze
    to_scale_divisions = to_scale_boil - to_scale_freeze
    return ((value-from_scale_freeze)*(to_scale_divisions/from_scale_divisions)+to_scale_freeze,
             to_scale)

my_favourite_temperature = (22, c)

print(convert(my_favourite_temperature, k))
# (295.15, 'K')
chapelo
  • 2,519
  • 13
  • 19