Input Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drizzit12
    New Member
    • Mar 2007
    • 2

    Input Help

    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.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by drizzit12
    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.
    Hello
    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
    Does that help?

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      Originally posted by ilikepython
      Hello
      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
      Does that help?
      try not to use "string" as a variable. string is a module by itself

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by ghostdog74
        try not to use "string" as a variable. string is a module by itself
        Oh ok, thanks for the advice.

        Comment

        • drizzit12
          New Member
          • Mar 2007
          • 2

          #5
          This fixed my problem. Thank you so much for your help.

          Comment

          Working...