Reputation:
New to Python, and have been given the following task by my teacher at school.
Trying to get average from 3x temperature inputs, and then to compare to a set value of 20 degrees, to return a message if average is < 20, or another message if >=20, but getting the following error message;
Traceback (most recent call last): File "main.py", line 18, in if avg < 20: TypeError: '<' not supported between instances of 'function' and 'int'
My coding is as follows;
# Ask for Temperature in Art Department
arttemp = input('Please Enter Art Department Temperature: ')
# Ask for Temperature in English Department
engtemp = input('Please Enter English Department Temperature: ')
# Ask for Temperature in Music Department
mustemp = input('Please Enter Music Department Temperature: ')
# This block calculates the average temperature
def avg(arttemp, engtemp, mustemp):
avg=(arttemp + engtemp + mustemp)/3
# This block displays the calculated average temperature
print ('The average temperature across the 3 departments is', avg)
# This decides if average temperature is less than 20 or not
if avg < 20:
print ('The Heating Should Be Turned On')
else:
print ('The Heating Should Be Turned Off')
Could anyone point out where I have gone wrong?
Thanks.
Upvotes: 0
Views: 2632
Reputation: 898
There are a few issues, but it's an easy fix.
First, input()
always returns a string. Since you're doing a numerical computation, you probably want to cast the input as a float
.
arttemp = float(input('Please Enter Art Department Temperature: '))
Second, the function avg()
doesn't actually return anything unless you use the keyword return
. Without a return
, a function returns the constant None
by default. Assigning the variable avg
within the function does not act as a return statement (like it sort of does in MATLAB, etc.). So you probably want the function to be
def avg(arttemp, engtemp, mustemp):
return (arttemp + engtemp + mustemp) / 3
Next, you need to make sure you call the function avg()
appropriately, for which you use parentheses. Using avg
without parentheses just refers to the function, but it doesn't ask the function to actually do anything (this is where the original error is coming from, you're trying to compare a function handle to an integer).
if avg(arttemp, engtemp, mustemp) < 20:
print ('The Heating Should Be Turned On')
Finally, you really don't need to write a function to compute the average (unless that was the point of the exercise) since you only call the function once. You can also use standard library functions to do this for you, like statistics.mean()
. Here's one way to put it all together.
# Get temperatures from the user.
arttemp = float(input('Please Enter Art Department Temperature: '))
engtemp = float(input('Please Enter English Department Temperature: '))
mustemp = float(input('Please Enter Music Department Temperature: '))
# Calculate and report the average.
avg_temp = (arttemp + engtemp + mustemp) / 3
print('The average temperature across the 3 departments is', avg_temp)
# Make a decision for the heating based on the average temperature.
if avg_temp < 20:
print('The Heating Should Be Turned On')
else:
print('The Heating Should Be Turned Off')
And, if you would like the names / number of departments to be a little more flexible, you could use a loop or list comprehension.
import statistics
departments = ["Art", "English", "Music"]
temperatures = [float(input(f"Please enter {d} department temperature: ")) for d in departments]
avg_temp = statistics.mean(temperatures)
print(f'The average temperature across the {len(departments)} departments is', avg_temp)
if avg_temp < 20:
print('The Heating Should Be Turned On')
else:
print('The Heating Should Be Turned Off')
Upvotes: 1