Correcting and "empty" or invalid response? Rather than just saying its invalid and moving on


#1



https://www.codecademy.com/courses/python-beginner-2W5v7/0/6?curriculum_id=4f89dab3d788890003000096#


The code is behaving, although I would like to know how to tell user to re-enter a word if it is invalid. Right now it appears as though the code only points out when input is invalid, however the user has no way of returning to input a valid response. How can I go about doing this?

I have personalized some of the responses and added a name input for the heck of it, which is where I notice the problem. If say, I type in an invalid name, there is no way of correcting it, the program simply moves on to the "Enter a word" prompt.


print 'Welcome to the Pig Latin Translator!'

name = raw_input("What's your name?")
if len(name) > 0 and name.isalpha():
    print name
else:
    print "Invalid"

original = raw_input("Enter a word:")
if len(original) > 0 and original.isalpha():
    print original
else:
    print "Invalid"


#2

Just include another input line after invalid like this,

print 'Welcome to the Pig Latin Translator!'

name = raw_input("What's your name?")
if len(name) > 0 and name.isalpha():
    print name
else:
    print "Invalid"

original = raw_input("Enter a word:")
if len(original) > 0 and original.isalpha():
    print original
else:
    print "Invalid"
    name = raw_input("What's your name?")

For it to be more intricate you would need to use maybe functions and you are not yet on that unit so for now this wll do but you can come back after learning about functions.:slight_smile:


#3

Right on, thanks @bandit


#4

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