In python, I am looking for some way to decipher an input by the user. If the user inputs "-1,2,1" (without the quotes), how can I break up the input into the numbers between the commas (ie a = -1, b = 2, and c = 1). Thank you.
Input Help
Collapse
X
-
Originally posted by drizzit12In python, I am looking for some way to decipher an input by the user. If the user inputs "-1,2,1" (without the quotes), how can I break up the input into the numbers between the commas (ie a = -1, b = 2, and c = 1). Thank you.
You can use the split function
Code:string = raw_input("Enter numbers") string = string.split(",") # this makes a list with every item in between the # given argument for x in range(len(string)): # make numbers into integers if you'd like string[x] = int(string[x]) try: # assign variables to the items in the list and make a = string[0] # sure program doesn't crash b = string[1] c = string[2] except IndexError: pass
-
Originally posted by ilikepythonHello
You can use the split function
Code:string = raw_input("Enter numbers") string = string.split(",") # this makes a list with every item in between the # given argument for x in range(len(string)): # make numbers into integers if you'd like string[x] = int(string[x]) try: # assign variables to the items in the list and make a = string[0] # sure program doesn't crash b = string[1] c = string[2] except IndexError: pass
Comment
-
Originally posted by ghostdog74try not to use "string" as a variable. string is a module by itselfComment
Comment