How to make the user input again after input the invalid one?


original = raw_input('Enter a word:')
if len(original) and original.isalpha():
    word = original.lower()
    print new_word
    print "Unvalid input"
    original = raw_input('Enter a word:')
above is my code
how to make the user type again and run the whole program


You could get rid of the last line of code and put everything in a while loop.


Option 1)
Surround it in a function, then run an if statement at the end asking if they want to go again. If they do, call the function again.

Option 2)
Surround it in a while loop or a for loop.
While Loop: Checking a boolean variable, change it to false if they don't want to continue.
For loop: Break from the loop if they don't want to continue - otherwise it will continue a set number of times.


Get it, thanks for your useful info.


Thanks, both of solutions are great.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.