Rogerio Ribeiro
Rogerio Ribeiro

Reputation: 1

Dividing the input in python

I'm doing a project where I need to insert coordinates in the console to return a place in a grid. My grid is 10*10 and has numbers in the rows and Letters in the columns. I want to be able to input something like A1 and for it to be interpreted as "column1, row1"

So far I have got:

def get_coor():
    user_input = input("Please enter coordinates (row,col) ? ")
    coor = user_input.split(" ")
    return coor

But I'm only able to split if I have a space. Is there any other function to help me in this situation?

Upvotes: 0

Views: 420

Answers (1)

FranciscoRZ
FranciscoRZ

Reputation: 92

Strings are iterable in Python.

If you write:

user_input = input("Please enter coordinates (row,col)?")
<input A1>

Then user_input[0] will be A and user_input[1] will be 1.

Therefore, no need for the split :) Split is used precisely for the use case when there is a space: it returns a list of all the strings between the occurrences of the character given as an argument (in your case a space).

Upvotes: 1

Related Questions