[Tutor] lists

Alan Gauld alan.gauld at yahoo.co.uk
Sat Nov 28 04:29:48 EST 2020


On 27/11/2020 21:09, nathan tech wrote:

> def is_great(l):
> 
>   pos=0
>   for x in range(l):

I assume you meant range(len(l))?

>    if(x==len(l)): 
>     return True

But that needs to be x == len(l)-2
due to zero indexing.

>   if(l[x]>l[x+1]):
>     return False

This would fail if you let x get above len(l)-2

> May need some slight bug checks on that function 

Yes indeed. That's why using range and indexing is so
frowned on in Python, its nearly always the wrong approach.
See my post for one way to do it without using range
and indexing.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list