-1

The assignment is:

  • Create an empty list called temperatures.
  • Allow the user to input a series of temperatures along with a sentinel value.
    (do not use a number for a sentinel value) which will stop the user input.
  • Evaluate the temperature list to determine the largest and smallest temperature.
  • Print the largest temperature.
  • Print the smallest temperature.
  • Print a message that tells the user how many temperatures are in the list.

The issue I am having is that if my list contains [-11, -44, -77] my program prints -11 as the lowest temperature. But I need it to print -77.

My code:

# Create a list called temperatures to capture user input
temperatures_list = []

# Create a while loop to capture user input into the temperatures_list<br />
if __name__ == "__main__":<br />
    while True:
        enter_number = (input("Please enter a temperature (enter stop to end): "))
        if enter_number == "stop": break
        temperatures_list.append(enter_number)
    lowest_temp = min(temperatures_list)
    highest_temp = max(temperatures_list)
    total_temps = len(temperatures_list)

    # Print output of temperatures input by user
    print(f"The numbers you input are:", temperatures_list)
    print(f"The lowest temperature you input is: ", lowest_temp)
    print(f"The highest temperature you input is: ", highest_temp)
    print(f"There are a total of {total_temps} temperatures in your list")
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
Trottd
  • 3
  • 1

3 Answers3

1

Convert the input to int before adding it to temperatures_list:

while True:
    enter_number = (input("Please enter a temperature (enter stop to end): "))
    if enter_number == "stop": break
    temperatures_list.append(int(enter_number))
PIG208
  • 2,060
  • 2
  • 10
  • 25
1

The problem you're having is that the numbers are in string format, so min() and max() aren't comparing the numerical values of the list values. This should do the trick:

# Create a list called temperatures to capture user input
temperatures_list = []

# Create a while loop to capture user input into the temperatures_list<br />
if __name__ == "__main__":<br />
    while True:
        enter_number = (input("Please enter a temperature (enter stop to end): "))
        if enter_number == "stop": break
        temperatures_list.append(int(enter_number))
    lowest_temp = min(temperatures_list)
    highest_temp = max(temperatures_list)
    total_temps = len(temperatures_list)

    # Print output of temperatures input by user
    print(f"The numbers you input are:", temperatures_list)
    print(f"The lowest temperature you input is: ", lowest_temp)
    print(f"The highest temperature you input is: ", highest_temp)
    print(f"There are a total of {total_temps} temperatures in your list")

As a side note, you could add in a try-except to catch cases where the user enters values that aren't a number, as this would cause your program to crash when converting the values to integer-type. For example:

# Create a list called temperatures to capture user input
temperatures_list = []

# Create a while loop to capture user input into the temperatures_list<br />
if __name__ == "__main__":<br />
    while True:
        enter_number = (input("Please enter a temperature (enter stop to end): "))
        if enter_number == "stop": break
        try:
            temperatures_list.append(int(enter_number))
        except ValueError:
            print('That wasn't a number! Try again)

    lowest_temp = min(temperatures_list)
    highest_temp = max(temperatures_list)
    total_temps = len(temperatures_list)

    # Print output of temperatures input by user
    print(f"The numbers you input are:", temperatures_list)
    print(f"The lowest temperature you input is: ", lowest_temp)
    print(f"The highest temperature you input is: ", highest_temp)
    print(f"There are a total of {total_temps} temperatures in your list")

float() would also work if you'd like to include numbers with decimals in.

Alfie Grace
  • 222
  • 1
  • 9
0

Create a function

def lowest_temp(temps):
    p = 0
    for i in temps:
        if i < i+1:
            p=i
    return p
Aaron Cloud
  • 315
  • 4
  • 15