Python Forum
How to continue in loop until correct input received
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to continue in loop until correct input received
#1
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?
Reply
#2
change
print("Sorry Wrong Format, Try Again Please")
to
break
Reply
#3
Sir I Want to ask the correct name until get. should not break by else
Reply
#4
(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?
Reply
#5
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

Attached Files

Thumbnail(s)
   
Reply
#6
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
Reply
#7
got Sir Thankyou Smile
Reply
#8
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?
Reply
#9
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.
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 990 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Code won't break While loop or go back to the input? MrKnd94 2 906 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,434 Apr-05-2022, 06:18 AM
Last Post: C0D3R
Question How to understand the received bytes of ser.read? pf2022 3 1,918 Mar-24-2022, 11:37 AM
Last Post: pf2022
Exclamation question about input, while loop, then print jamie_01 5 2,615 Sep-30-2021, 12:46 PM
Last Post: Underscore
  Attribute Error received not understood (Please Help) crocolicious 5 2,611 Jun-19-2021, 08:45 PM
Last Post: crocolicious
  Stop/continue While loop block Moris526 68 24,820 Mar-28-2021, 09:21 PM
Last Post: Larz60+
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 2,719 Nov-23-2020, 11:01 PM
Last Post: perfringo
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 2,882 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes
  Python - Most effective way to correct keyboard-user-input. ppel123 8 4,059 Apr-08-2020, 07:41 AM
Last Post: ppel123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020