In: Computer Science
(Python) In a weather station, there is a sensor that measures
the temperature three times a
day (in Celsius). Write a program that asks the user to input three
numbers, corresponding to the
sensor's three readings for a particular day. Then, print the
minimum, maximum and average value
of the three numbers.
Note: If one or two inputs are either less than -70, or greater
than +50 degrees, you should ignore
those one or two inputs, and calculate the minimum, maximum and
average using only the other
inputs. If all 3 inputs are either less than -70 or greater than
50, your code should print "Broken
sensor!" and not perform any calculations.
For this question, you must not use any built-in functions or
functions from math
module except print(), input() or oat(). You must compute the
minimum, maximum
and average values using if statement and arithmetic operators.
This means that you
cannot use the built-in max() or min() functions.
Code -
#variable declare
flag1 = 0
flag2 = 0
flag3 = 0
#maximum and minimum temperature to be stored
maximum = 100
minimum = 100
#ask user to enter temperature first
print("Enter temperature first ")
num1 = int(input())
if num1>50 or num1<(-70) :
flag1 = 1
num1 = 0
#ask user to enter temperature second
print("Enter temperature second ")
num2 = int(input())
if num2>50 or num2<(-70) :
flag2 = 1
num2 = 0
#ask user to enter temperature third
print("Enter temperature third ")
num3 = int(input())
if num3>50 or num3<(-70) :
flag3 = 1
num3 = 0
#if conditon to get the maximum temperature
if num1>num2 and num1 > num2 and flag1==0 :
maximum = num1
elif num2>num1 and num2 > num3 and flag2==0 :
maximum = num2
elif flag3==0 :
maximum = num3
#if conditon to get the minimum temperature
if num1<num2 and num1 < num2 and flag1 == 0 :
minimum = num1
elif num2<num1 and num2 < num3 and flag2==0 :
minimum = num2
elif flag3==0 :
minimum = num3
#do total
total = num1+num2+num3
#calculate average
avg = total/3
#print result
if maximum != 100:
print("Maximum temperature is "+str(maximum))
else:
print("Broken Sensor!")
if minimum != 100:
print("Minimum temperature is "+str(minimum))
if maximum != 100:
print("Average temperature is "+str(avg))
Screenshot -