0

I am trying to create a continuous user input but do not want it to restart from the start if a wrong info is put in. For e.g.,

a = 1
while a ==1:
    Number = raw_input("Number: ")
    if len(Number) != 3:
        print "\n Error, please enter 4 digits \n"
    else:    
        Day = raw_input("Day: ")
        if not Day.isdigit():
            print "\n Error, please enter day in digits \n"

Here, if the person enters a non-4 digit for the first input, the loop will end and go back to the start again. However, if he gets to the second input and enters a non-digit string, the loop will also end and go back to the start. How do I get it to not go back to the start but ask for the Day again?

3
  • The condition of the loop a==1 never becomes false because a never changes. If you want to end the loop, change a. Commented Feb 22, 2018 at 7:28
  • The whole point of a loop is that it goes back to the start, that's why it's a loop.. perhaps you don't need a loop? Please clarify your question Commented Feb 22, 2018 at 7:28
  • I do not want to end the loop as I want the user to continue entering data and I have an option for the user to end the loop manually. Perhaps I should not use a loop and that is my question: any other options to do what I want? Commented Feb 22, 2018 at 7:40

5 Answers 5

3
def myMethod():
    a = 1
    while True:
        if a is 1 and len(raw_input("NUMBER: ")) is not 3:
            print "ERROR IS HERE IN NUMBER"
        else:
            a = 2
            if a is not 1 and raw_input("DAY: ").isdigit() is True:
                a = 1
            else:
                print "ERROR IS HERE IN DAY"
myMethod()

you can use this for your purpose. key thing is you need to maintain a key for checking if there is error in DAY or in NUMBER.

Please let me know if you need any other clarification. this will ask you DAY: and NUMBER: infinitely

Sign up to request clarification or add additional context in comments.

Comments

2

You can simplify this by function:

def input_number(prompt, digits_num=0):
    while 1:
        s = raw_input(prompt)

        if not s.isdigit():
            print "\n Error, please input digits \n"
            continue

        if digits_num:
            if len(s) != digits_num:
                print "\n Error, please enter %d digits \n" % digits_num
                continue

        result = int(s)
        break

    return result


number = input_number("Number: ", 4)
day = input_number("Day: ")

print "\n number=%d, day=%d" % (number, day)

1 Comment

This works great. And I can use it for other user inputs. Thanks a lot.
1

You need two separate while loops for that purpose. One for number and the other one for day. Here is the corrected code:

number = ""
day = ""

while True:
    number = raw_input("Number: ")
    if not number.isdigit() or len(number) != 4:
        print "Error, Please enter a 4 digit number \n"
    else:
        break


while True:
    day = raw_input("Day: ")
    if not day.isdigit():
        print "\n Error, please enter day in digits \n"
    else:
        break

print "Number: ", number
print "Day: ", day

Comments

1

If you can use Python 3, Use Enum in such scenarios for a production level code. You can always manipulate the conditions according to your needs. Here we go :

from enum import Enum

class Status(Enum):
    START = 1
    ‎MIDDLE = 2
    ‎END = 3

state = Status.START

while state != Status.END:
    while state == Status.START:
    ‎    Number = input("Number:")
    ‎    if len(Number) != 3:
    ‎        print("Invalid Number !")
    ‎    else:
    ‎        state = Status.MIDDLE

    while state == Status.MIDDLE:
    ‎    Day = input("Day:")
    ‎    if not Day.isdigit():
    ‎        print("Invaid Day !")
    ‎    else:
    ‎        state = Status.END 
    ‎

Comments

-3

Raise Exception until valid input

a = 1
while a ==1:
    try:
        Number = input("Number: ")
        if len(Number) != 3:
            print("\n Error, please enter 4 digits \n")
            raise RuntimeError()
    except RuntimeError:
        continue
    break

2 Comments

Please format your code appropriately and do not use exceptions where a simple assignment can do the job.
replace the raise RuntimeError() with continue ... get rid of the try/except

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.