samedi 7 décembre 2019

What is the point of setting variables to True or False?

I am watching a tutorial video on Youtube about Python basics. During the video he creates a lists and writes a code to print out "Found" if there are any words beginning with the letter "J". enter image description hereHere is the code:

names = ["John", "Mary"]
found = False
for name in names:
    if name.startswith("J"):
        print("Found")
        found = True
        break
if not found:
    print("Not found")

My question is why he decided to set the variable found to False at the beginning of the code if he then changes it to True towards the bottom?

Aucun commentaire:

Enregistrer un commentaire