That code is half right.
The if
part is correct, because as soon as you find any punctuation, your goal is met and you don't need to keep looking, so you can correctly break
out of the loop.
However the else
part is wrong, because if the current character is not punctuation, you still have to keep looking -- the remainder of the string might have punctuation. Also the message is wrong; it should say the string doesn't have punctuation.
You can restructure the code to take advantage of the for/else
construct:
string = input("Enter a string")
for char in string:
if char in "'.,;:?!":
print("that string contains punctuation")
break
else:
print("that string does not contain punctuation")
The else
will execute if the loop makes it all the way to the end of the string without calling break
.