A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

what's wrong with this code ? (syntax?) from string import * def findFirst (target, key): x = find('target','key') x = x+1 y = find('target','key',x) print y return

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Hey Carl ! how are ya ? ;)

  2. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    You need to show us what you're passing when you call the function, but looking at what you've posted: You don't need to import anything from string, .find() is a built in method, it's always available. When you call .find(), you have to pass a string as the first argument, which you have, but not the way I think you wanted to. Your function takes two arguments, which it assigns to a variable named target and one named key. When you call .find(), you must call it on a string, as in a = 'some big long string' b = a.find('substring') This assigns an integer to `b` which is the index position of the beginning of the first occurrence of the substring in `a`. You're calling the find method on the string assigned to 'a' when you do a.find() The method returns where in `a` the string passed to the method, in this case 'substring', occurs. It'll return -1 if the substring doesn't occur in `a`. You can pass two more optional arguments, but these must be integers, not a string, as you've got above. find('target', 'key') What you need to do is something like def findFirst (target, key): position = x.find(target, key) # note: no quotes, and were assuming that key is an int ## x = x+1 # you can't increment a string?? y = find(target, key, x) # again, no quotes print y return # what is this for? You only need a return statement if you're returning something other than None. Like return (x, y) If `x` is a string, doing y = 'abc' x.find(y, 5, 19) will look for where 'abc' first appears in the string, x[5:19]. There's a bunch of stuff to look at. Have a go at squaring things up a bit and let us know how you get on.

  3. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Hi grupiyati, I'm very well thank you, yourself?

  4. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Reading over my post I didn't make it very clear, let me know what bits you don't get and we'll go over them. That post is all over the place :)

  5. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    What do you want the function to do? If you only want to find the first occurrence of one string in another, that's what the .find() method is for.

  6. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ok, now what I have is from string import * def fF(target,key): position = x.find(target,key) y = find(target,key,x) print y,x which I

  7. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    which I totally understand ... but the error I'm getting is: >>> fF(abcd,bc) Traceback (most recent call last): File "<pyshell#29>", line 1, in <module> fF(abcd,bc) NameError: name 'abcd' is not defined >>> which i dont understand :)

  8. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    What you're doing is confusing strings with names. In your function, you have two names that are assigned to whenever it is called. The names, target and key, do not have 'quotes' because they are not strings of text, they are names. This is what you've done, so it's fine, but when you pass arguments to the function by doing >>>fF(abcd, bc) You're also using unquoted arguments, which defines them as names, not strings. If I have the following function def say_hi(nickname): print 'Hello', nickname you will notice that 'Hello' has quotes because it's a string, but nickname never has quotes, because it's a variable name. Now I can call the function, passing a string directly >>>say_hi('Bob') # note the quotes, it is a string I'm passing Hello Bob or I can pass a name that'll evaluate to a string >>>a = 'Bob' >>>say_hi(a) # note; no quotes. it is a variable I'm passing that evaluates to a string Hello Bob If I do >>>say_hi(Bob) I'll get NameError: name 'Bob' is not defined because the is no name Bob. So I could do this >>>Bob = 'Jim' >>>say_hi(Bob) Hello Jim

  9. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    You don't need to import from string, there's nothing in your script that isn't built in. Have you looked at using IPython? It takes some setting up, but I'd be happy to help you get it running nicely for learning Python, assuming you're on Linux. IPython is a good tool for creating functions and playing around with them, the standard Python Interpreter's interactive mode really sucks.

  10. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    You can't call find() as a function. It's a method, which is very similar to a function, but must be called on an instance of something ~ list methods must be called on a list, string methods are called on strings. Every string in your code has the string methods 'attached to it'. To call a method, you use the same parentheses and argument passing syntax and it works in the same way, but you must call the method 'on a string'. >>>a = 'abc' >>>print a.upper() ABC Or I could do >>>print 'abc'.upper() ABC but I can't do print upper(a) because upper is a string method and must be called on a string.

  11. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Sorry ~ I have just realised that find() is a function within the string module, so you can call it the way you were calling it before. Why they have a find function in string and a string method, I'll never know, but you can call find() the way you were doing it. If you are only pulling in one or two functions from a module, it can be better to name them from string import find I thought you'd just got the method call syntax wrong. Sorry about that. Chat soon :)

  12. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    got it got it :) haha i'm at ps3b and c now :)

  13. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Good stuff mate.

  14. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.