SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    subtracting from strings

    So I have a string called "mainString1" and it has a list consisting of "pearls diamonds jewels rocks" I have another that contains the input from the user called "enteredList" and I want to subtract anything from "mainString1" that is entered by the user so if they entered "pearls" then it prints "diamonds jewels rocks"

    I'm not sure of all that is wrong with the following
    Code:
    if enteredList.find(mainString1) != 1:
        print "You still need the following:"
        print mainString1 - enteredList
    Also how would I count to make it read "You still need 3 of the following" or change the number to the appropriate amount needed?

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    Bangalore / Patna, India
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dbzakj View Post
    So I have a string called "mainString1" and it has a list consisting of "pearls diamonds jewels rocks" I have another that contains the input from the user called "enteredList" and I want to subtract anything from "mainString1" that is entered by the user so if they entered "pearls" then it prints "diamonds jewels rocks"

    I'm not sure of all that is wrong with the following
    Code:
    if enteredList.find(mainString1) != 1:
        print "You still need the following:"
        print mainString1 - enteredList
    Also how would I count to make it read "You still need 3 of the following" or change the number to the appropriate amount needed?
    you should stop writing program.

  3. #3
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by satya prakash View Post
    you should stop writing program.
    Please don't be rude. That's not constructive or helpful.

    dbzakj, what language is this? It looks like python, is that what you're using?

    It is doubtful the subtraction operator is defined for strings (what it means to subtract a string is ambiguous, you could want it to do any number of things). You'll actually have to write code to make it do what you want, which is to replace that substring if it's present and perhaps some spaces as well.

    Perhaps you should work with a data structure other than a string? Put your list into an actual list or array. That would make it easier to examine the items one at a time to see if they match the user input. Python has string.split() which would let you create a list from a string, given a space (' ') as the delimiter.

    http://docs.python.org/library/stdtypes.html

    Then you can use list.remove() to remove the desired item, and list.count() to tell the user how many are in the list.

    http://docs.python.org/tutorial/data...#more-on-lists

  4. #4
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah it's Python, for this specific code I have to deal with working with strings. I don't believe there is a substring to this.

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    str.replace() and str.count() will do the job, then; see above for the documentation link

  6. #6
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you give an example of those? I don't think I know how they really work.

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That's why I linked you to the webpage which explains how they work

    enteredList = enteredList.replace('pearls', '')


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •