-2

I am currently studying python, and I am struggling to find an answer to an assignment. I need to create a list from a user input, then find the highest temperature of the input and count how many times that temperature appears from the input. Finally I should output the highest temperature and how many times it appears.

input_temps = []
for i in range(int(input("Enter the tmeperature here: "))):
    input_temps.append(int(input()))

print(input_temps)

this is what I have so far.

Edit: I am not allowed to use any max or count functions

King kong
  • 3
  • 3
  • Looks like you take first temperature value as the count itself. – Austin Jan 12 '20 at 17:23
  • Does this answer your question? [How to find all positions of the maximum value in a list?](https://stackoverflow.com/questions/3989016/how-to-find-all-positions-of-the-maximum-value-in-a-list) – Moein Kameli Jan 12 '20 at 17:42
  • @Piron They don't need the positions, and going that route would make things unnecessarily complicated. – Kelly Bundy Jan 12 '20 at 17:44
  • Which part of the problem are you struggling with? Your title isn't very representative/accurate, by the way, since creating the list is the one thing you already have figured out. – AMC Jan 12 '20 at 18:09

3 Answers3

2

(Note: This was before they changed the question to rule out those functions, but a solution without is at the end)

You can use max and count functions. And better improve the input messages:

input_temps = []
for i in range(int(input("Enter the number of temperatures: "))):
    input_temps.append(int(input("Enter a temperature: ")))
print(input_temps)
max_temp = max(input_temps)
print(f'{max_temp=}')
max_temp_count = input_temps.count(max_temp)
print(f'{max_temp_count=}')

Demo:

Enter the number of temperatures: 3
Enter a temperature: 5
Enter a temperature: 4
Enter a temperature: 5
[5, 4, 5]
max_temp=5
max_temp_count=2

Since you now asked for a version without using max:

input_temps = []
max_temp = None
max_temp_count = None
for i in range(int(input("Enter the number of temperatures: "))):
    temp = int(input("Enter a temperature: "))
    input_temps.append(temp)
    if max_temp is None or temp > max_temp:
        max_temp = temp
        max_temp_count = 1
    elif temp == max_temp:
        max_temp_count += 1
print(f'{max_temp=}')
print(f'{max_temp_count=}')
Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65
  • What would the be the more difficult way without using the max_function? – King kong Jan 13 '20 at 15:55
  • When i run this it doesn't go past the entering of the input "Enter the number of temperatures". i am a complete noob when it comes to python, i can understand the code and how it should run but i still am struggling – King kong Jan 16 '20 at 13:40
  • What do you mean with "it doesn't go past"? – Kelly Bundy Jan 16 '20 at 13:43
  • I was expecting the code to then ask me to enter the temperatures to add to the list then find the max and count and finally output those answers, so once i clicked enter the code stops – King kong Jan 16 '20 at 13:47
  • I don't think that's possible, unless you used the second version and answered the first question with `0`. – Kelly Bundy Jan 16 '20 at 13:49
  • I used the second version only and when asked to enter the number of temperatures i entered '5' pressed enter then the code stops – King kong Jan 16 '20 at 13:53
  • That can't be right. I think you changed the code somehow. – Kelly Bundy Jan 16 '20 at 13:54
  • Yes you are right, what would be the next step to print the output on screen to say what temperature is the highest with how many times in appears? – King kong Jan 16 '20 at 14:05
1

Another possibility is to enter all the temperatures, either space or comma separated:

temps = input('Enter the temperatures here: ')

temps_list = [float(s) for s in temps.split(',')]

hg = max(temps_list)
cnt = temps.count(hg)
print(f'List of temperatures: {temps}\
      \nThe highest temperature is {hg}, which appears {cnt} times.')

Working example:

Enter the temperatures here: 3,1.1,2,3.2,6,2.2,6,5.7,5
List of temperatures: [3.0, 1.1, 2.0, 3.2, 6.0, 2.2, 6.0, 5.7, 5.0]      
The highest temperature is 6.0, which appears 2 times.
FBruzzesi
  • 6,385
  • 3
  • 15
  • 37
0

To find the max temperature in the list, just use max(input_temps). After that, you can set up a counter variable starting at 0 and use a for loop that iterates through each element in the list and compares it to the max temperature. If they are equal, just add 1 to the counter.

Richard X
  • 1,119
  • 6
  • 18