Reputation: 83
I have this code:
guess = list()
playerNumber = raw_input
('Please enter a four digit number for which all the digits are different.')
p = str(playerNumber)
guess = [int(p[0]),int(p[1]),int(p[2]),int(p[3])]
I am trying to convert the raw input of a four digit number given by the user into a list of individual integers. My code is not working. How can I do this?
Upvotes: 0
Views: 3345
Reputation: 1
A bit more independence can be provided to the user in terms of entering the list of integers in a comma(or any other delimiter) separated way -
listOfIntRaw = raw_input("Enter the numbers separated by comma - ")
listOfInt = list(map(int,listOfIntRaw.split(',')))
Upvotes: 0
Reputation: 63777
I would have simply done, without compromising readability. Python built-in map, applies the call back to each element of a sequence and or iterable. Here it calls int
to convert each of the elements of the sequence to integers. Another thing to note is, a string is a iterable, can be iterated over the individual characters
playerNumber = raw_input('Please enter a four digit number for which all the digits are different.')
playerNumber = map(int, playerNumber)
Upvotes: 4