简体   繁体   中英

Average temperature in seven days, min and max temps

I am doing an exercise: input temperature for 7 days, calculate the average and find the minimum and maximum temperatures.

#input and Calculate the average temperature for 7 days.
#Find the maximum and minimum temperature
print("Welcome!")
max = 0
minimum = 999
d1 = int(input("Enter Sunday Temperature: "))
d2 = int(input("Enter Monday Temperature: "))
d3 = int(input("Enter Tuesday Temperature: "))
d4 = int(input("Enter Wednesday Temperature: "))
d5 = int(input("Enter Thursday Temperature: "))
d6 = int(input("Enter Friday Temperature: "))
d7 = int(input("Enter Saturday Temperature: "))

average = (d1 + d2 + d3 + d4 + d5 + d6 + d7) / 7

if d1>max:
    max = d1
if d2>max:
    max = d2
if d3>max:
    max = d3
if d4>max:
    max = d4
if d5>max:
    max = d5
if d6>max:
    max = d6
if d7>max:
    max = d7

if d1<min:
    min = d1
if d2<min:
    min = d2
if d3<min:
    min = d3
if d4<min:
    min = d4
if d5<min:
    min = d5
if d6<min:
    min = d6
if d7<min:
    min = d7


print("Average Temperature =", average)
print("Maximum Temperature =", max)
print("Minimum Temperature =", min)

I get this error message:

TypeError: '<' not supported between instances of 'int' and 'builtin_function_or_method'

Any time you see repeated code like that, there is a better way. Let the computer do the work for you.

#input and Calculate the average temperature for 7 days.
#Find the maximum and minimum temperature
print("Welcome!")
d = [
    int(input("Enter Sunday Temperature: ")),
    int(input("Enter Monday Temperature: ")),
    int(input("Enter Tuesday Temperature: ")),
    int(input("Enter Wednesday Temperature: ")),
    int(input("Enter Thursday Temperature: ")),
    int(input("Enter Friday Temperature: ")),
    int(input("Enter Saturday Temperature: "))
]

average = sum(d) / 7
dmax = max(d)
dmin = min(d)

print("Average Temperature =", average)
print("Maximum Temperature =", dmax)
print("Minimum Temperature =", dmin)

Without getting too "golfy" there is a lot of redundant code in your current solution and I think it is making it much harder to both write and understand.

Let's shorten it up a bit and hopefully add some clarity.

Note that we are going to use slightly more descriptive variable names as your actual issue at the moment is a collision between the variable name and builtin method min (and that you have redefined the builtin max ).

days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
observations =[int(input(f"Enter {day} Temperature: ")) for day in days]

temp_average = sum(observations) / len(observations)
temp_max = max(observations)
temp_min = min(observations)

print(f"Average Temperature = {temp_average}")
print(f"Maximum Temperature = {temp_max}")
print(f"Minimum Temperature = {temp_min}")

If you were keen on keeping your current solution, then you can adjust the variable names slightly:

#input and Calculate the average temperature for 7 days.
#Find the maximum and minimum temperature
print("Welcome!")
maximum = -999
minimum = 999
d1 = int(input("Enter Sunday Temperature: "))
d2 = int(input("Enter Monday Temperature: "))
d3 = int(input("Enter Tuesday Temperature: "))
d4 = int(input("Enter Wednesday Temperature: "))
d5 = int(input("Enter Thursday Temperature: "))
d6 = int(input("Enter Friday Temperature: "))
d7 = int(input("Enter Saturday Temperature: "))

average = (d1 + d2 + d3 + d4 + d5 + d6 + d7) / 7

if d1>maximum:
    maximum = d1
if d2>maximum:
    maximum = d2
if d3>maximum:
    maximum = d3
if d4>maximum:
    maximum = d4
if d5>maximum:
    maximum = d5
if d6>maximum:
    maximum = d6
if d7>maximum:
    maximum = d7

if d1<minimum:
    minimum = d1
if d2<minimum:
    minimum = d2
if d3<minimum:
    minimum = d3
if d4<minimum:
    minimum = d4
if d5<minimum:
    minimum = d5
if d6<minimum:
    minimum = d6
if d7<minimum:
    minimum = d7

print("Average Temperature =", average)
print("Maximum Temperature =", maximum)
print("Minimum Temperature =", minimum)
print("Welcome!")
max = 0
minimum = 999
arr = []
d1 = int(input("Enter Sunday Temperature: "))
arr.append(d1)
d2 = int(input("Enter Monday Temperature: "))
arr.append(d1)
d3 = int(input("Enter Tuesday Temperature: "))
arr.append(d1)
d4 = int(input("Enter Wednesday Temperature: "))
arr.append(d1)
d5 = int(input("Enter Thursday Temperature: "))
arr.append(d1)
d6 = int(input("Enter Friday Temperature: "))
arr.append(d1)
d7 = int(input("Enter Saturday Temperature: "))
arr.append(d1)

average = (d1 + d2 + d3 + d4 + d5 + d6 + d7) / 7

for x in arr:
   if x > max:
      max = x
   if x < min:
      min = x


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM