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


#1



original = raw_input('Enter a word:')
if len(original) and original.isalpha():
    word = original.lower()
    first=word[0]
    new_word=word[1:]+first+"ay"
    print new_word
else:
    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


#2

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


#3

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.


#4

Get it, thanks for your useful info.


#5

Thanks, both of solutions are great.


#6

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