Python Forum
Need help returning min() value of list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help returning min() value of list?
#1
Howdy,

I have a feeling that my issue lies within my second function. The Max function works for returning the single highest digit however the min function returns a blank value

lst = []


def sort_max():
    max(lst)
    return (sorted(max(list(lst)))[-1])

def sort_min():
    min(lst)
    return (sorted(min(list(lst)))[+1])

while True:
    usrInput = (input("List "))
    if usrInput == "":
        print("Min", sort_min(), "Max", sort_max())
        break
    lst.append(usrInput)
Reply
#2
I'm a bit confused as to what you are wanting to do. So, posing some questions that are aimed at getting you to think. Looking at lines 6 and 10,
1. What do you think list(lst) will do for you?
2. Does max(any list) give you a list that can be sorted, or a single item?

Suggest you look at what the result of each step would be by a series of print statements. I think that will show you the answer
Reply
#3
Sorry for the confusion posted this a little late. So what i need is for lst to store user input of multiple numbers in one line e.g. the user should be able to input 8 7 6 5 4 3 2 1 in one line with spaces.

Then i need to it to print the highest/lowest digit entered e.g. 8 and 1. Right now it only points the 8 and will blank out on the 1 if i enter the exampled input
Reply
#4
Here some hint,first step is to get user input to be a list of integers.
Then will min() and max() do what name hint about,without using sorted() or other stuff.
>>> usr_input = input("Enter numbers with space between: ")
Enter numbers with space between: 8 7 6 5 4 3 2 1

>>> usr_input
'8 7 6 5 4 3 2 1'
>>> usr_input.split()
['8', '7', '6', '5', '4', '3', '2', '1']

# To make a list with integers 
>>> lst = [int(i) for i in usr_input.split()]
>>> lst
[8, 7, 6, 5, 4, 3, 2, 1]
>>> max(lst)
8

>>> print(f'Min numer is <{min(lst)}> Max number is <{max(lst)}>')
Min numer is <1> Max number is <8>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list.sort() returning None SmallCoder14 8 498 Mar-19-2024, 09:49 PM
Last Post: SmallCoder14
  returning a List of Lists nafshar 3 1,056 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  2d List not returning DariusKsm 2 1,687 Sep-22-2020, 05:11 PM
Last Post: DariusKsm
  API call returning list value of 'None' jimbone30 5 2,557 Jun-14-2019, 07:42 PM
Last Post: jimbone30

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020