0

Write a Python program to input 12 temperature values (one for each month) and display the number of the month with the highest temperature

maxTemp = 
maxMonth = 0

for mon in range(12):
    temp = float(input("Enter temperature: "))
    if temp > maxTemp:
       maxTemp = temp
       maxMonth = mon
         
print('The maximum temperature of {} occured in month {}'.format(maxTemp,maxMonth))

Hello, if I write import sys and maxTemp=sys.float_info.min, it works but is there any way for this problem to solve it by not using sys or any special modules?

likt
  • 3
  • 1
  • Does this answer your question? [Maximum and Minimum values for ints](https://stackoverflow.com/questions/7604966/maximum-and-minimum-values-for-ints) – Adam.Er8 Aug 26 '20 at 15:05
  • 1
    TL;DR: `maxTemp = float('-inf')` should do the trick – Adam.Er8 Aug 26 '20 at 15:06

2 Answers2

0

One super simple way you can do it is have a special first case:

maxTemp = None
maxMonth = 0

for mon in range(12):
    temp = float(input("Enter temperature: "))
    if maxTemp is None or temp > maxTemp:
       maxTemp = temp
       maxMonth = mon
M Z
  • 4,571
  • 2
  • 13
  • 27
0

here is another solution:

import pandas as pd

collector = []
for i in range(12):
    for j in range(1):
        i = float(input("Enter temperature: "))
        j = float(input("Enter month: "))
        collector.append([i,j])

columns = ['temp','month']
df = pd.DataFrame(collector, columns = columns)
result = df.iloc[df.temp.argmax(), 0:2]

print('The maximum temperature of {} occured in month {}'.format(result[0],round(result[1])))
Saikat
  • 1
  • 1