This question already has an answer here:
In my Python code, I have a part where I would like to repeatedly print a question if it doesn't match what I want it to.
name1 = input("A male name (protagonist): ")
if name1.endswith (('ly', 's')):
print("Sorry mate, this doesn't seem to be a proper noun. Try it again.")
name1 = input("A male name (protagonist): ")
How do I make it repeatedly print out name1 if it ends with 'ly' or 's'?
I think this is what you are after:
This will loop over until
if name1.endswith(('ly', 's'))
is true.You can use a
while
loop. While loops will continuously perform something as long as it's specified condition is true.If you don't want a name that ends with
ly
ors
, you can make awhile
loop like so:I should also mention that because the
if
statement was satisfied, it will restart the loop. Once the name does not end withly
ors
, it will move to theelse
block, which will come out of the loop. Note that thebreak
is the word that forces out of the loop.