This is my code:
my_Sentence = input('Enter your sentence. ')
sen_length = len(my_Sentence)
sen_len = int(sen_length)
while not (sen_len < 10 ):
if sen_len < 10:
print ('Good')
else:
print ('Wo thats to long')
break
I'm trying to make the program ask the user continuously to write a sentence, until it is under 10 characters. I need to know how to have the program as for a sentence again, but I think the simplest way would be to have the code start from the top; but I'm not surte how to do that. Can someone help?
The pattern
The general pattern for repeatedly prompting for user input is:
We use the infinite loop
while True:
rather than repeating ourget_user_input
function twice (hat tip).If you want to check the opposite case, you simply change the location of the
break
:If you need to do work in a loop but warn the user when they provide invalid input then you just need to add a test that checks for a
quit
command of some kind and onlybreak
there:Mapping the pattern to your specific case
You want to prompt a user to provide you a less-than-ten-character sentence. This is an instance of pattern #2 (many invalid responses, only one valid response required). Mapping pattern #2 onto your code we get: