can only concatenate list (not "str") to list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amankillz
    New Member
    • Oct 2012
    • 1

    can only concatenate list (not "str") to list

    My program is supposed to reverse the string inputted, print it then extract the numbers and print the numbers of the reversed string in a list.

    Code:
    def reverse_str(string):
        revstring =('')
        length=len(string)
        i = length - 1
        while i>=0:
            revstring = revstring + string[i]
            i = i - 1
        return revstring
    def strip_digits(string):
        result = []
        for c in string:
            result = result + c
    string = raw_input("Enter a string->")
    new_str = reverse_str(string)
    print new_str
    numberless_str  = strip_digits(string)
    Last edited by Rabbit; Oct 16 '12, 04:46 AM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    You've told us what you want to do. Now what's your question?

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      can only concatenate list (not "str") to list
      You should have included the complete error message which includes the offending line.

      "+" concatenates 2 lists. To add another string to an existing list use append (see "list methods"). Note also that you pass "string" to strip_digits(), not "new_str". Finally do not use "string" as a variable name as string is already used by python.

      Comment

      Working...