Home > Mobile >  Trying to understand this block of python code
Trying to understand this block of python code

Time:07-27

I am new to python and I am learning this word counter exercise. The code is all correct.

What I don't understand is the instance of string defined function. Why are there string1 and string2 in the parameters? If I run the code and type in "hello, how are you?", isn't that just 1 string? But the code runs fine and I get a result of 4 words. I am having difficulty understanding how this code block works.

I also just want to confirm, is the main() function that is defined used to start the programming with the input loop even though it is at the bottom of the codes? Thank you for your help in advance.

def word_counter(sentence):<br>
    """Counts the number of words in a given sentence"""

    sentence = sentence.strip() #removes whitespace from beginning and end of sentence.

    num_spaces = count_instance_of_str(sentence, ' ')

    #counting the amount of space   1 as total words.        
    word_count = num_spaces   1

    return word_count

def count_instance_of_str(string1, string2):<br>
    """Counts characters in string1 that are also in string2."""

    count = 0

    #for each char in string1, check if it's in string2
    for char in string1:
        if char in string2:
            count  = 1

    return count
def main():

    #creates an infinite loop
    while 1 == 1:

        input_string = input("enter a string: ")

        if input_string == '-1':
            break

        print(word_counter(input_string), "words in", input_string)

#to execute main function<br>
if __name__ == '__main__': (name and main has __ before and after)<br>
    main()

CodePudding user response:

You defined a function count_instance_of_str(string1, string2). It has two parameters, the first of which will be named string1 inside the function, and the second string2. When you execute the line num_spaces = count_instance_of_str(sentence, ' '), this function gets "invoked", or "called", and sentence is passed as the first parameter, while ' ' is passed as the second. By the very same mechanism, sentence is, within the word_counter function, the value of input_string in main. Thus, the function count_instance_of_str will start with string1 = "hello, how are you?", and string2 = ' '.

You can read more about functions at Python.org tutorial.

CodePudding user response:

Look at what is being passed to def count_instance_of_str(string1, string2).

You are sending:

sentence and ' '(one space string) to num_spaces = count_instance_of_str(sentence, ' ').

def count_instance_of_str(string1, string2): Loops through the characters in your sentence 'hello, how are you?' and checks for each character if it is present in string2 a.k.a. ' ' (one space string).

Since this string only contains a space, count = 1 will only run when the character in string1 is a space

So count goes from 0 to 3 and 3 is returned and then assigned to num_spaces, which then 1 into word_count which gives 4

num_spaces = count_instance_of_str(sentence, ' ')

#counting the amount of space   1 as total words.        
word_count = num_spaces   1

return word_count
  • Related