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