0

I'm trying to add validation to my application in Python.

Basically I want to make sure their input is a number and if its not, it should just keep asking them the input question until they get it.

This is what I got which only works for like 2 tries, I believe its because it checks if its not null.

I know this is a lot of code. Not sure of another way to do this, any ideas?

while amount != "":
    try:
        val = int(amount)
        while counter < int(amount):
            counter = counter + 1
            ran = ran + 1
            num3 = input(str(ran) + ". Input: ")
            try:
               val = int(num3)
            except ValueError:
               num3 = input(str(ran) + ". Input: ")
            numbers.append(num3)
        print("")
        home()
    except ValueError:
        amount=input("How many numbers are in your list? ")
        while counter < int(amount):
            counter = counter + 1
            ran = ran + 1
            num3 = input(str(ran) + ". Input: ")
            try:
               val = int(num3)
            except ValueError:
               num3 = input(str(ran) + ". Input: ")
            numbers.append(num3)
        print("")
        home()
3
  • What does the home function do? Commented Oct 28, 2013 at 16:47
  • Just restarts. But that doesn't got much to do with the error. Commented Oct 28, 2013 at 16:47
  • I'm not sure what the function is supposed to do as there seems to be some nested asking for values. The same code twice seems to hint that you are doing more complicated things than necessary. Commented Oct 28, 2013 at 16:50

1 Answer 1

1

how bout

def get_int_input(txt):
    x = raw_input(txt)
    while not x.isdigit():
        x = raw_input("Invalid Input. %s"%txt)
    return int(x)

numbers = [get_int_input("%s input:"%i) for i in range(get_int_input("Enter Number Of values:"))]

note that this is based on my best guess of what you are trying to do, and I may not be understanding what you are trying to do

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

Comments

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.