Python Forum

Full Version: How to continue in loop until correct input received
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi Guys,
Pls help me out !
while(1):
            fname=input("Enter Father's First name")
            fname = fname.lower()
            if(fname.isalpha()):
                addfname(fname)
                fname+=" "
            else:
                print("Sorry Wrong Format, Try Again Please")
in this if Name is not input correctly then else breaks and send the user out instead of asking again correct name.
So How same can be done?
change
print("Sorry Wrong Format, Try Again Please")
to
break
Sir I Want to ask the correct name until get. should not break by else
(May-03-2019, 08:29 PM)sunnyarora Wrote: [ -> ]if Name is not input correctly then else breaks and send the user out instead of asking again correct name.

(May-03-2019, 08:50 PM)sunnyarora Wrote: [ -> ]Sir I Want to ask the correct name until get. should not break by else

Sorry but you need to explain better what it is you want, what defines a correct name?
Sir,
Basically i want that program should ask the correct name until same is received from user.
program should not go to next line of code or break the program.

as shown in attachment
Nope still don't understand.
dont_break_program = True # program should not break the program

while True:
    ask_correct_name = input('ask correct name') # program should ask the correct name
    if ask_correct_name == 'same': # until same is received from user
        while dont_break_program:
            pass
        
        next_line = 'program wont go here' # program should not go to next line of code
got Sir Thankyou Smile
while True:
ask_correct_name = input('ask correct name')  # program should ask the correct name
    if ask_correct_name == 'same':  # until same is received from user
          while True:
               pass
Quote:Sir,
Please explain about second's while True.
What is it doing / Why is it used for?
The last code I posted is not necessarily what you want but is one way your requirements can be interpreted due to their vagueness.
while True: creates an endless loop because True will always be True.
Quote:Sir, by which code loop is getting terminated if right input received?

Error:
Sir if I applied below code in my program then function is showing unresolved name (not linking with function)
        while True:
            fname=input("Enter Father's First name")
            fname = fname.lower()
            if(fname.isalpha()):
                addfname(fname)
                while True:
                    pass
Whereas below code is working fine
        while True:
            name=input("Enter Customer First Name")
            name=name.lower()
            if(name.isalpha()):
                addname(name)
                while True:
                    pass
Pages: 1 2