1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Hi Guest, welcome to the TES Community!

    Connect with like-minded education professionals and have your say on the issues that matter to you.

    Don't forget to look at the how to guide.

    Dismiss Notice

Finding word matches in Python

Discussion in 'Computing and ICT' started by uselessnerdII, Feb 21, 2017.

Tags:
  1. uselessnerdII

    uselessnerdII New commenter

    I need to create an algorithm that checks input text for a word and matches it from a predefined list. I have had a look at some tutorials and they are all a bit too complex. The group I am teaching this to will only be able to manage something with simple code (and to be honest I am fairly thick myself so I will need something rudimentary!). Any help would be very much appreciated
     
  2. binaryhex

    binaryhex Lead commenter

  3. madcat

    madcat Occasional commenter

    In python you can just use
    if xxx in yyy

    So something like
    wordlist=["one", "two",,"three"]:
    newword = Input("what's the word ?")
    if newwword in wordlist:
    print"found"
     
  4. neddyfonk

    neddyfonk Lead commenter

    Yep, so much for the prophecy in 1972 that spoke of all computer languages in the future having conversational English formats. COBOL was about as near as we got with PERFORM SEARCH VARYING COUNTER FROM ONE BY ONE UNTIL SEARCH_COMPLETE which makes C / Perl / Python seem clumsy to me.
     
  5. madcat

    madcat Occasional commenter

    Yes, I'd have to agree that "under the hood" we haven't made any great progress.But the UI has come on leaps and bounds. Sitting here listening to a selection of music by Django Reinhardt, initiated by just asking Alexa for it makes me believe that I actually am starting to live in the 21st C.

    Still waiting for the monorails though
     
  6. colwynexile

    colwynexile Occasional commenter

    and hoverboards...
     
  7. LinkToThis

    LinkToThis New commenter

    text = input("Enter your text")
    text = text.lower()
    text = text.split()

    if "word" in text:


    print("Words")

    elif "another" in text:


    print("Another what?")

    else:


    print("error")
     
  8. theworm123

    theworm123 Lead commenter

    That code makes no sense
     
  9. LinkToThis

    LinkToThis New commenter

    Which part doesn't make sense exactly?

    It is a simple program that searches for a word inside of a series of strings.
     
  10. theworm123

    theworm123 Lead commenter

    I have a few bullet points of some constructive criticism I hope will help you;
    • The 'split()' function is unnecessary, there isn't the need to convert the string to a list.
    • Split() isn't necessary because Python allows you to directly search the string directly.
    • The 'lower()' function while necessary in this program can be implemented within the variable itself.
    • The 'elif' statement can be removed by using 'or' as an alternative. In your program, only 1 test is ever going to be true and the other 2 false, I have narrowed it down to having only 1 true and 1 false by using 'or'. 'or' is similar to doing this in C-syntax languages;
    Code:
    int main()
    {
        int a = 0;
    
        if(a != 1 || a != 2)
        {
           printf("bad");
        }
        else
        {
           printf("success!");
        }
    }
    
    The primary reason I said it makes no sense are the odd choices such as the split() function and the program is very long winded, I refactored it down from 9 to 6 lines by removing unnecessary functionalities and oddities. The modifications can be seen below:

    Code:
    text = input("Enter your text: ").lower()
    
    if "word" in text or "another" in text:
        print("Success")
    else:
        print("error")
    
    Your example is very 'textbook-ish'.

    Code:
    text = input("Enter your text") 
    text = text.lower()
    text = text.split()
    
    if "word" in text:
        print("Words")
    
    elif "another" in text:
        print("Another what?")
    
    else:
        print("error")
    
     
  11. LinkToThis

    LinkToThis New commenter

    I mean, were teaching kids here, not making super efficient programs, you can do a lot of programs in much simpler ways, but at some point it stops being beneficial for students or other teachers trying to learn, and turns into a pissing match on who can program better.

    I use .split() as the last few controlled assessment's made heavy use of arrays and lists, I have tailored my teaching to that, focusing more on for loops ect.

    There is also a massive point you are missing, where students will find my solution much easier than yours, and will get the same amount of marks.

    Also I am not sure why you are showing this problem in another language other than to pat your own ego, considering OP asked for help with Python, not other languages.

    It helps to see programs being done in different ways, example;

    Code:
    
    word = "This is a string"
    if word.find("Hello") == -1:
          print("Not here")
    else
         print("Found")
    
    Would I teach them this method over .split? No not really, its overly confusing.
     
  12. theworm123

    theworm123 Lead commenter

    This isn't a good example to teach lists, a student will learn nothing about lists from your code. If I were to use this to teach the semantics of lists, I would declare the list from the outset, with either the words already entered as list elements or better yet, use append() to show the words being added to the list. Iteration can be taught with better examples.

    This isn't an excuse of 'Oh look at me I'm the better programmer', not being able to produce efficient code is a serious problem with new programmers at large. Adding in convoluted, unnecessary steps such as split() in this exampls is just daft. If you had actually done something with the list which you can't do with a standard string I'd understand.

    And the accusation of 'ego-stroking' is a rather arbitrary claim. What I wrote I used to illustrate my point, that other language was C by the way. I know more than Python, so what? I don't show off about that fact, there literally isn't a programmer in the industry who knows a single language. That simply isn't sustainable.

    My post was meant for you not the OP, frankly I think my own code would be easier to understand, it contains less fluff and does the job. I just think unnecessary functions leads to pisspoor examples.
     
  13. dmcdon13

    dmcdon13 New commenter

    Suggestion to theworm123 and LinkToThis: could you two please stop your public bashing and maybe put your efforts into helping other teachers who aren't confident in using code, develop their knowledge and understanding. This behaviour is a rather poor representation of those who can code. I for one left the teaching profession to become a professional developer, after having my interest sparked by the changes in the curriculum from ICT to CS. I am doing my bit by volunteering at meet ups etc to give back to education. Maybe that would be a good source to focus all of this energy...but in a positive way.

    Long story short - stop acting like the children that you teach.

    In the outside world there is no 'right' way to write code - it needs to be functional and easy for other developers to read/understand. If it efficient and correctly tested - even better!
     
  14. LinkToThis

    LinkToThis New commenter

    The fact you feel the need to make your comments shows that you want everyone to pat you on your back and go well done.

    This is why I dislike programming teachers who worked in industry, as they teach either too much content, or are so focused on making "Efficient" code, because that's what the industry wants they forget they are teaching children.

    We don't criticize children for not meeting industry standards in other GCSE's, but industry standard teachers within CS will always do this.

    Also yes I know its C, again, not sure why you are posting this other than to stroke your ego once more, were all very impressed you know more than one language, or were you so used to being the few who knows more than python you expected me to be shocked?

    No you just randomly feel the need to show off your knowledge when the conversation isn't even about C? Okay then.

    I teach children to use .split() because it is the plan I take them on, where the next step is to store each individual element within a list to be written to a file. You are challenging me based purely on your own opinion on what makes a good python program.

    And that Is my problem, you have an idea about what a good program should be, because of your experience in the industry, but that doesn't necessarily make it suitable for children, or even needed at GCSE.

    Your method is perfectly fine, however so is mine for GCSE, and depending on the scheme of work the students are on, both have its advantages and disadvantages.
     
  15. LinkToThis

    LinkToThis New commenter

    I fully agree with you, however as it is within each school, many people feel the need to stroke their own egos.

    I do help out with my academy chain and go through the curriculum with other schools, however there are always some who hate being taught by someone else or someone younger than they are, and refuse to let their ego be bruised.

    its the same principle with industry programmers, sometimes the right way for a child and the right way for a industry program do not align.
     
  16. theworm123

    theworm123 Lead commenter

    Not going to keep on arguing about this but I don't need to show off the fact that I know C, frankly I don't care that I do, I learned it as part of my undergraduate project and have barely it since. My program isn't how they would do it in the industry, they would use find(). Industry programmers are preferred because we know the language we are teaching, I'm not a lesson ahead of my students.
     
  17. JaquesJaquesLiverot

    JaquesJaquesLiverot Established commenter

    If anything, I would say that the opposite is true - in industry there's often a desire for readability to enhance maintainability and team working, but when approaching Computer Science as an academic discipline, I would say that efficiency is the most important thing - it's pretty-much the raison d'être of the whole subject, and I encourage my students to program efficiently from the moment they start to use Scratch.

    I'm so bemused by the antipathy of Computing teachers towards efficiency that I wrote a blog on it - Are You a Computer Scientist?
     
    theworm123 likes this.
  18. theworm123

    theworm123 Lead commenter

    Achieving the most efficient code possible in Python is also very important because it's an interpreted language as opposed to compiled, it is quite slow.

    I'm very upfront about this because my research field is based around creating extremely efficient generic algorithms, I didn't meant to offend you if I did @LinkToThis but like @JaquesJaquesLiverot says in the academic field of Computer Science most of the research applies to finding the most efficient algorithm to carry out a task.

    Even trying to find a more efficient algorithm for Hamiltonian paths is still popular.
     
    Last edited: Jun 28, 2017

Share This Page