Python List / Index Query
I am asking a user to enter a number, in which I want to print the item held at that index point in the list..
This is the code i currently have:
List = ["a", "b", "c", "d", "e", "f"]
print "The list has the following", len(List), "list:", List
new_item = 开发者_运维技巧raw_input("Which item would you like to add? ")
List.append(new_item)
print "The list has the following", len(List), "items:", List
Number = raw_input ("Please select a number: ")
Try converting Number
to an integer first:
i = int(Number)
print "You selected:", List[i]
Incidentally, it's good Python style to make variables lower case, and keep identifiers that begin with a capital letter for classes. So, instead of List
you could use my_list
and instead of Number
just use number
. (You shouldn't use list
as a variable name since that will hide the built-in list
type.)
l = ["a", "b", "c"]
ii = raw_input("Please select a number: ")
print l[ii]
What you are attempting to do is called "indexing" a string. It is achieved as follows
letter_at_index = my_list[index]
Note that indices start from 0, that is,
my_list = ['a', 'b', 'c']
a = my_list[0]
b = my_list[1]
c = my_list[2]
Negative indices can be specified as well, for example
c = my_list[-1]
More info under Sequence Types here
List = ["a", "b", "c", "d", "e", "f"]
print "The list has the following", len(List), "list:", List
try:
Number = raw_input ("Please select a number: ")
item = List[int(Number)]
print "The list item at", Number, "is", item
except EOFError:
print "Error. Nothing entered"
except ValueError:
print "Error. Expecting a number instead of:", Number
except IndexError:
print "Error. Number out of range:", Number
List = ["a", "b", "c", "d", "e", "f"]
print "The list has the following length: "+str( len(List))+ "\n list:"+repr( List)
List.append(raw_input("\nWhich item would you like to add? "))
print "\nThe list has the following length: "+str( len(List))+ "\n list:"+repr( List)
print '\nThe item at the position you entered is : '+\
repr(List[int(raw_input ("\nPlease select a number: "))])[1:-1]
Result
The list has the following length: 6
list:['a', 'b', 'c', 'd', 'e', 'f']
Which item would you like to add? (12,52,145)
The list has the following length: 7
list:['a', 'b', 'c', 'd', 'e', 'f', '(12,52,145)']
Please select a number: 6
The item at the position you entered is : (12,52,145)
try the following:
i = None
listend = len(List)-1
while i == None:
try:
print ''
raw = raw_input("Which item would you like to add? ")
i = int(raw)
value = List[i]
except IndexError:
print i, 'is out of bounds. Expecting an integer from 0 to', listend
i = None
except:
print 'You entered "%s" however I was expecting an integer from 0 to %s'%(raw, listend)
else:
print 'Result:', List[i]
It is often useful to loop while waiting for acceptable input in this way - you will notice that i
is only set to something other than None
if int(raw)
runs successfully, and if it is out of your list's bounds you set it back to None
. The else
statement is not strictly necessary as its content could go outside of the while
loop, however it seem to be the situation it was designed for - running a block of code only if the try block has not thrown an exception, while not handling exceptions itself.
精彩评论