'String comparison in python not working inside the function
can someone explain me why the equality operator is not working inside this function?
def count_words(words_to_insert, word):
counter = 0
for i in range(0,words_to_insert):
random_word = input("Insert a string ")
print(random_word)
print(random_word == word)
if(random_word == word):
counter +=1
print(counter)
return "You inserted the word " + word + " " + str(counter) + " times"
count_words(2, "mango")
If I write two strings outside the function with the same values and I compare them, the result is True
string_one = "mango"
string_two = "mango"
string_one == string_two
OUT
True
Solution 1:[1]
Your code works. Just enter mango without quotes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dfull |

