1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

I strugle with this code.... in python

Discussion in 'AI - Artificial Intelligence in Digital Marketing' started by Florin94, Jul 15, 2017.

Tags:
  1. Florin94

    Florin94 Registered Member

    Joined:
    Sep 29, 2016
    Messages:
    73
    Likes Received:
    0
    Hi all I struglle with this code in python. I try to make a function that can find a world from a phrase.

    Function look like this :

    def find(strng,ch):
    ... index=0
    ... while index<len(strng):
    ... if strng[index]==ch:
    ... return index
    ... index=+1
    ... return-1

    What i posted is copy/paste more from a book but I dont understand basically what does when I write this : "if strng[index]==ch" more exactlly why is put "index" in this type of parantheses? From what I learn this paranthese[ ] is use for list and to find element from a world for example s="ban" then s[0] will be =b but now in function I dont know why use this paranthese and what purpose have index here. Can someone explain please? thank
     
  2. TwoOneThree

    TwoOneThree Regular Member

    Joined:
    Feb 2, 2017
    Messages:
    313
    Likes Received:
    38
    Gender:
    Male
    You'll find more useful answers at Stackoverflow
     
  3. sanehatter

    sanehatter Newbie

    Joined:
    Sep 2, 2016
    Messages:
    3
    Likes Received:
    1
    Gender:
    Male
    Most likely the function takes a string as parameter, hence the parameter name "strng", iterates over each character of the string with the "while index < length of the string" and returns the index ( location in the string) of the first occurrence of ch. In python, lists and strings are considered iterable objects, and each individual element can be accessed via indexing, which is done through [ ].
    Hope this helps.
     
    • Thanks Thanks x 1
  4. Florin94

    Florin94 Registered Member

    Joined:
    Sep 29, 2016
    Messages:
    73
    Likes Received:
    0
    Yes but when I try to put at work this function doesnt do anything .......also no eroor
     
  5. Florin94

    Florin94 Registered Member

    Joined:
    Sep 29, 2016
    Messages:
    73
    Likes Received:
    0
    Yes now I understand the function search for index of the ch in strng..... But hovewer dont do anythng this function ..... not show the index position of ch
    Also I try to configure something that can search for ex "a" in "banana" and count . So far I was thinking at write:
    W="banana"
    if "a" in W:
    count+=1
    print count

    But this codde get syntax error. How should I code this? thank
     
  6. sanehatter

    sanehatter Newbie

    Joined:
    Sep 2, 2016
    Messages:
    3
    Likes Received:
    1
    Gender:
    Male
    you should read the python docs and check stackoverflow, maybe you have some indentation errors, i don't know. If you want to find a letter in a string, use string.find
     
  7. oseart

    oseart Registered Member

    Joined:
    Oct 30, 2015
    Messages:
    62
    Likes Received:
    26
    Why don't you just use a deliminator to separate the phrase so you can parse the word/words you need? That would be a lot easier.
     
  8. RazeByte

    RazeByte Junior Member

    Joined:
    Mar 13, 2014
    Messages:
    129
    Likes Received:
    106
    Gender:
    Male
    Occupation:
    Student
    This is extremely basic and if you can't understand this, you probably didn't read the book properly or understand it before reaching whatever section you're on...
     
  9. Florin94

    Florin94 Registered Member

    Joined:
    Sep 29, 2016
    Messages:
    73
    Likes Received:
    0
    I understand how that function work but now Im struglle with this function:
    def remove_vowels(s):
    ... vowels="aeiouAEIOU"
    ... s_without_vowels=" "
    ... for letter in s:
    ... if letter not in vowels:
    ... s_without_vowels+=letter
    ... return s_without_vowels
    More specific with "s_ithout_vowels+=letter" . From my knowledge "+=" is to incrementing. Why is used here and how exactlly work? I was thinking to use just "+" in this case. Can anyone explain this? Thank you for all
     
  10. RazeByte

    RazeByte Junior Member

    Joined:
    Mar 13, 2014
    Messages:
    129
    Likes Received:
    106
    Gender:
    Male
    Occupation:
    Student
    "+" is used in arithmetic operations but can also be used in some languages to 'concatenate/join' strings (text) together. In this case, all they're doing is adding the letter on to the string by using "+". All it means to use something like = after an operation is to make it update the variable itself on top of doing the operation. For example:

    Z = 0;
    Z += 10;
    Z is now 10, this is the shorter form way of saying:
    Z = Z + 10;

    So in the context of strings, it would be doing something like:

    X = "Hello";
    X += " World";