0

Beginner coder here trying to get past a challenge. I'm trying to write a program that prompts the user for a list of numbers and then prints the maximum and minimum values from that list. I realize that there are built in functions to do this in Python but I have defined my own to try and better grasp the logic and how the language works. Here is what I have so far:

def find_largest_number(nums):
    largest_number = nums[0]
    for number in nums:
        if number > largest_number:
            largest_number = number
    return largest_number


def find_min_number(nums):
    min_number = nums[0]
    for number in nums:
        if number < min_number:
            min_number = number
    return min_number


nums = []
while True:
    num = input("> ")
    nums.append(num)
    if num == 'done':
        break
    try:
        value = int(num)
    except ValueError:
        print('Invalid input')
        continue

print(find_largest_number(nums))
print(find_min_number(nums))

If I run this code I can successfully get the minimum number, however the break statement 'done' is always returned as the largest number. Is there a way to exclude it from the functions or something else that I am missing? I have tried adding an int() before the num in both of the functions but this just causes the program to crash with the following error: TypeError: '>' not supported between instances of 'int' and 'str'

location:

def find_largest_number(nums):
    largest_number = nums[0]
    for number in nums:
        if int(number) > largest_number:
            largest_number = number
    return largest_number
Shanahando
  • 61
  • 5

1 Answers1

1

You want to move the line nums.append(num) so that it goes right after the line value = int(num) and then edit it so that it reads nums.append(value) instead.

Currently, you are appending the input num to the list before checking to see if it is really the signal value "done". You really want to wait until after verifying that it is not "done" before appending.

Secondly, you want to convert the num to an actual number (an int) which you are already doing when you say value = int(num). Now, value is the integer (numeric) version of the input, and that is what you really want to append to your list.

The problem with not converting the num from a string to an int is that the comparison operators like < and > work differently on strings and ints. Strings are compared using alphabetical order, and so, for example, 'b' goes after 'aa'. But if these were numbers, we would want the opposite behavior, since 2 should come before 11.

Andrew Merrill
  • 1,672
  • 11
  • 14
  • Not sure why this was closed for some other reason. Andrew's right here; it's about appending the input only after you've evaluated it to be a number you want to compare. – Shawn Ramirez Feb 17 '22 at 20:52
  • In this code ```num``` and ```number``` are both strings which is why you get the type error when ```num == `done'``` and trying to ```int(num)```. My approach would be to validate the input right at the stage of input so that the rest of the input processing is with integer type. – InhirCode Feb 17 '22 at 20:55
  • I have no idea why it was closed either, apparently it was associated with another question that isn't the same or anywhere near as specific. I appreciate the help though folks! Thanks for the detailed explanations – Shanahando Feb 18 '22 at 06:28
  • @ShawnRamirez do you mean writing at this statement: num = int(input("> ") - ? I tried this and it resulted in the program crashing if I tried to enter the break statement 'done' – Shanahando Feb 18 '22 at 06:41