Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren0
Why this break statement not work?
In this code break statement not in loop why? And when it is in loop i want to print only "yes" but why it prints "None" also? https://code.sololearn.com/cee5TGEAD6Ef/?ref=app
3 Antworten
+ 2
Aayush Jat If you haven't a loop you can't use "break" or "continue" keywords.
Instead you can use "pass" (or "return" if you are inside a function) to have in part the same behaviour
Try with
def search(text,word):
if word in text:
return "yes"
return
def search(text,word):
if word in text:
return "yes"
def search(text,word):
if word in text:
return "yes"
else:
pass
def search(text,word):
if word in text:
print("yes")
and so on...
+ 1
Where is the loop in your code ?
+ 1
None is printed as it is the return value of your function, and you print the result of its call in addition to printing 'yes' inside the function ^^
break only work in loop and if line is reached (inside an if..else statement, it depend of the condition where it is ;P