Python Forum
How to continue in loop until correct input received - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to continue in loop until correct input received (/thread-18052.html)

Pages: 1 2


How to continue in loop until correct input received - sunnyarora - May-03-2019

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?


RE: How to continue in loop until correct input received - Yoriz - May-03-2019

change
print("Sorry Wrong Format, Try Again Please")
to
break



RE: How to continue in loop until correct input received - sunnyarora - May-03-2019

Sir I Want to ask the correct name until get. should not break by else


RE: How to continue in loop until correct input received - Yoriz - May-03-2019

(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?


RE: How to continue in loop until correct input received - sunnyarora - May-03-2019

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


RE: How to continue in loop until correct input received - Yoriz - May-03-2019

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



RE: How to continue in loop until correct input received - sunnyarora - May-03-2019

got Sir Thankyou Smile


RE: How to continue in loop until correct input received - sunnyarora - May-04-2019

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?



RE: How to continue in loop until correct input received - Yoriz - May-04-2019

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.


RE: How to continue in loop until correct input received - sunnyarora - May-04-2019

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