[Tutor] Trying to get next item from a list

Joel Goldstick joel.goldstick at gmail.com
Mon Sep 17 18:14:06 CEST 2012

On Mon, Sep 17, 2012 at 12:04 PM, Santosh Kumar <sntshkmr60 at gmail.com> wrote:
> Here is the script:
> alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
> 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
> 'z']
> i = input("Press any English alphabet: ")

You ask for a letter, but you are using it as an index.  In ascii, the
letter 'a' is represented by the number 97.  The rest of the alphabet
goes up from there.

First you want to be sure that you are getting a lower case letter.
Look in the documentation for string methods to see how that is done.
Make sure that i is lower case.
then using the ord('a') function you will get 97.  If you subtract
ord('a') from ord(i) you will get the index that you call current.
But be careful because if your user choses 'z', you will end up with a
character that is out of range in your list.   You could either say
that is the end, or wrap back around to a

> current = alphabets.index(i)
> print(current)
> next = current+1
> print(next)
> print(alphabets.index(next))
> I am getting a ValueError.
> And as you can see, I just trying to get the next item. So is there
> any way I can get same functionality in less line of codes?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Joel Goldstick

More information about the Tutor mailing list