afeefkhateeb afeefkhateeb - 28 days ago 10
Python Question

Break outside the loop

I am new to Python. I am trying to run the following code. But every time I try to run it, the IDE says that the break is outside the loop

catname = []

print("Enter the name of the cats")

name = input()

if name == '':

break

catname = catname+[name]

print("The cat Names are :")

for catname in name:

print(name)


Can you please help me?

Thanks

Answer

You use break when you want to break free from a loop, to exit the loop, to jump to the nearest code after the loop.

Your code doesn't contain a loop, so nothing to break free from, hence the error.