Beginner Python Help

Chris Warrick kwpolska at gmail.com
Fri Mar 18 03:23:54 EDT 2016


On 18 Mar 2016 08:05, "Alan Gabriel" <alanunny at gmail.com> wrote:
>
> Hey there,
>
> I just started out python and I was doing a activity where im trying to
find the max and min of a list of numbers i inputted.
>
> This is my code..
>
> num=input("Enter list of numbers")
> list1=(num.split())
>
> maxim= (max(list1))
> minim= (min(list1))
>
> print(minim, maxim)
>
>
>
> So the problem is that when I enter numbers with an uneven amount of
digits (e.g. I enter 400 20 36 85 100) I do not get 400 as the maximum nor
20 as the minimum. What have I done wrong in the code?

You're dealing with strings (text) and not integers. And when comparing
strings, '1' is earlier than '2', thus '100' < '20'.

To fix this, you can use a list comprehension:

list1 = [int(i) for i in num]

You also don't need (parentheses) around functions when assigning to
variables:

maxim = max(list1)
minim = min(list1)

(Also, list1 is not a good variable name. Try something that describes its
contents.)

-- 
Chris Warrick <https://chriswarrick.com/>
Sent from my Galaxy S3.



More information about the Python-list mailing list