I strugle with this code.... in python

Florin94

BANNED
Joined
Sep 29, 2016
Messages
194
Reaction score
15
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
 
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.
 
Yes but when I try to put at work this function doesnt do anything .......also no eroor
 
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.

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
 
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
 
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.
 
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...
 
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
 
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

"+" 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";
 
Back
Top