Reputation: 101
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??
Upvotes: 1
Views: 1434
Reputation: 2562
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')
Upvotes: 0
Reputation: 822
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.
Upvotes: 0
Reputation: 19733
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
Upvotes: 2
Reputation: 871
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)
Upvotes: 2
Reputation: 14619
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.
Upvotes: 0