Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #105183 > unrolled thread
| Started by | Alan Gabriel <alanunny@gmail.com> |
|---|---|
| First post | 2016-03-18 00:04 -0700 |
| Last post | 2016-03-18 00:50 -0700 |
| Articles | 6 — 6 participants |
Back to article view | Back to comp.lang.python
Beginner Python Help Alan Gabriel <alanunny@gmail.com> - 2016-03-18 00:04 -0700
Re: Beginner Python Help Terry Reedy <tjreedy@udel.edu> - 2016-03-18 03:20 -0400
Re: Beginner Python Help Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-18 09:44 +0200
Re: Beginner Python Help Chris Warrick <kwpolska@gmail.com> - 2016-03-18 08:23 +0100
Re: Beginner Python Help Ben Finney <ben+python@benfinney.id.au> - 2016-03-18 18:26 +1100
Re: Beginner Python Help "Martin A. Brown" <martin@linux-ip.net> - 2016-03-18 00:50 -0700
| From | Alan Gabriel <alanunny@gmail.com> |
|---|---|
| Date | 2016-03-18 00:04 -0700 |
| Subject | Beginner Python Help |
| Message-ID | <101edd27-17e1-497c-a60f-fa56b033563b@googlegroups.com> |
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?
Thanks for the help
[toc] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2016-03-18 03:20 -0400 |
| Message-ID | <mailman.303.1458285656.12893.python-list@python.org> |
| In reply to | #105183 |
On 3/18/2016 3:04 AM, Alan Gabriel 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")
input returns a string
> list1=(num.split())
list1 is a list of strings
> maxim= (max(list1))
> minim= (min(list1))
min and max compare the strings as strings, lexicographically
> 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 failed to convert strings of digits to ints. Try
list1 = map(int, num.split)
This will raise TypeError on strings that cannot be converted.
--
Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jussi.piitulainen@helsinki.fi> |
|---|---|
| Date | 2016-03-18 09:44 +0200 |
| Message-ID | <lf537roxmb0.fsf@ling.helsinki.fi> |
| In reply to | #105184 |
Terry Reedy writes: > On 3/18/2016 3:04 AM, Alan Gabriel wrote: ... >> list1=(num.split()) > > list1 is a list of strings > >> maxim= (max(list1)) >> minim= (min(list1)) > > min and max compare the strings as strings, lexicographically > >> print(minim, maxim) ... > You failed to convert strings of digits to ints. Try > > list1 = map(int, num.split) > > This will raise TypeError on strings that cannot be converted. Written in a hurry? :) Surely it'll raise something about a method not being iterable, and when that is fixed, something about not being able to invent a minimum for an empty sequence. list1 = list(map(int, num.split()))
[toc] | [prev] | [next] | [standalone]
| From | Chris Warrick <kwpolska@gmail.com> |
|---|---|
| Date | 2016-03-18 08:23 +0100 |
| Message-ID | <mailman.304.1458285836.12893.python-list@python.org> |
| In reply to | #105183 |
On 18 Mar 2016 08:05, "Alan Gabriel" <alanunny@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.
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2016-03-18 18:26 +1100 |
| Message-ID | <mailman.305.1458286014.12893.python-list@python.org> |
| In reply to | #105183 |
Alan Gabriel <alanunny@gmail.com> writes: > 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. Welcome to Python! As a Python beginner you will be interested to join the ‘python-tutor’ forum <URL:https://mail.python.org/mailman/listinfo/tutor> which is focussed specifically to collaborative teaching of beginner Python. -- \ “If history and science have taught us anything, it is that | `\ passion and desire are not the same as truth.” —E. O. Wilson, | _o__) _Consilience_, 1998 | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | "Martin A. Brown" <martin@linux-ip.net> |
|---|---|
| Date | 2016-03-18 00:50 -0700 |
| Message-ID | <mailman.306.1458287449.12893.python-list@python.org> |
| In reply to | #105183 |
Greetings Alan and welcome to Python,
>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?
I will make a few points, as will probably a few others who read
your posting.
* [to answer your question] the builtin function called input [0]
returns a string, but you are trying to get the min() and max()
of numbers; therefore you must convert your strings to numbers
You can determine if Python thinks the variable is a string or
a number in two ways (the interactive prompt is a good place to
toy with these things). Let's look at a string:
>>> s = '200 elephants'
>>> type(s) # what type is s?
<class 'str'> # oh! it's a string
>>> s # what's in s?
'200 elephants' # value in quotation marks!
The quotation marks are your clue that this is a string, not a
number; in addition to seeing the type. OK, so what about a
number, then? (Of course, there are different kinds of numbers,
complex, real, float...but I'll stick with an integer here.)
>>> n = 42
>>> type(n) # what type is n?
<class 'int'> # ah, it's an int (integer)
>>> n # what's in n?
42 # the value
* Now, perhaps clearer? max(['400', '20', '36', '85', '100'])
is sorting your list of strings lexicographically instead of
numerically (as numbers); in the same way that the string
'rabbit' sorts later than 'elephant', so too does '85' sort
later than '400'
* it is not illegal syntax to use parentheses as you have, but you
are using too many in your assignment lines; I'd recommend
dropping that habit before you start; learn when parentheses are
useful (creating tuples, calling functions, clarifying
precedence); do not use them here:
list1 = (num.split()) # -- extraneous and possibly confusing
list1 = num.split() # -- just right
* also, there is also Tutor mailing list [1] devoted to helping
with Python language acquisition (discussions on this main list
can sometimes be more involved than many beginners wish to read)
I notice that you received several answers already, but I'll finish
this reply and put your sample program back together for you:
num = input("Enter list of numbers: ")
list1 = list(map(int, num.split()))
print(list1)
maxim = max(list1)
minim = min(list1)
print(minim, maxim)
You may notice that map [2] function in there. If you don't
understand it, after reading the function description, I'd give you
this example for loop that produces the same outcome.
list1 = list()
for n in num.split():
list1.append(int(n))
The map function is quite useful, so it's a good one to learn early.
Good luck,
-Martin
[0] https://docs.python.org/3/library/functions.html#input
[1] https://mail.python.org/mailman/listinfo/tutor/
[2] https://docs.python.org/3/library/functions.html#map
--
Martin A. Brown
http://linux-ip.net/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web