-1
digits = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']

def inp():
    inp = input('Enter a value: ')

    for i in inp:
        if i not in digits:
            print('Please enter a value from 2 to 100')
            inp()

    n = int(inp)
    if not 2 <= n <= 100:
        print('Please enter a value from 2 to 100')
        inp()
    else:
        return n

number = inp()

I am unable to make it foolproof: user can enter an empty string "" or float, and it gives an error.

For example: If I enter 10.5 and then 10 then it shows error that 10.5 cannot be taken as integral value.

If there would be a simple function like val() from VB6 or something equivalent for python, I would be very grateful.

Edit: Expected behaviour: I want the code to take only integral value, 2 to 100 as input, and if incorrect value is entered then it should repeat until an appropriate value is received.

Please help.

Brian M Stafford
  • 8,483
  • 2
  • 16
  • 25
Ishan Jindal
  • 198
  • 1
  • 2
  • 18
  • I have to re-ask since https://stackoverflow.com/questions/69658320/error-while-making-foolproof-input-method-using-for-loop was closed and my problem was not solved. – Ishan Jindal Oct 21 '21 at 12:00
  • What is the purpose of the full-proof test? Please describe the expected behavior (or your question might be closed again) – mozway Oct 21 '21 at 12:01
  • you can do: `try int(input)`, did you try that? – Mahrkeenerh Oct 21 '21 at 12:02
  • Also, avoid the recursive approach, an infinite loop with a break on condition is much better – mozway Oct 21 '21 at 12:03
  • "infinite loop with a break on condition" --- I am unaware of that, fellow coder Are you referring to "while True break"?? – Ishan Jindal Oct 21 '21 at 12:08
  • the main problem is solved. anyways, is there an val() equivalent function from VB6 in Python, or in some library i can import? – Ishan Jindal Oct 21 '21 at 13:23

2 Answers2

0

I found the solution to be this:

def inp_w():
    while True:
        try:
            inp = round(float(input('Width of the maze (number of blocks): ')))
        except:
            print('Please enter a value from 2 to 100')
            continue
        else:
            break
    
    if not 2 <= inp <= 100:
        print('Please enter a value from 2 to 100')
        inp_w()
    else:
        return inp


a = inp_w()
Thanks to
"https://stackoverflow.com/users/16343464/mozway"
"https://stackoverflow.com/users/13000953/mahrkeenerh"
"https://stackoverflow.com/users/9635106/karl-wilhelm"
"https://stackoverflow.com/users/16601101/jeandemeusy"
Ishan Jindal
  • 198
  • 1
  • 2
  • 18
-1

There is three errors you are making:

  • you named a method and a variable the same way. The interpreter doesn't know what to do when it comes to "inp()". Are you trying to call method "inp" or the variable "inp" ? One of them is impossible to do, so it crashes. As y good practice, never give the same name to different element in your code.
  • you are missing the character '.' in your list of digits. When parsing the string "10.5" it doesn't recognizes the dot, and thus crashes.
  • the conversion from string to int is not able to convert "10.5" to 10, because of the floating point part. So convert it to a float instead of an int.

Here is a working code:

valid_chars = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."]


def input_val():
    inp = input("Enter a value: ")

    for i in inp:
        if i not in valid_chars:
            print("Please enter a value from 2 to 100")
            return input_val()

    n = float(inp)
    if not 2 <= n <= 100:
        print("Please enter a value from 2 to 100")
        return input_val()

    return n


number = input_val()

Edit: A better answer, without recursion could be something like this:

def is_float(string):
    try:
        float(string)
        return True
    except:
        return False


def input_val(min_val: int, max_val: int):

    while True:
        inp = input("Enter a value: ")

        if not is_float(inp):
            print("Please enter a number")
        else:
            n = float(inp)
            if not min_val <= n <= max_val:
                print(f"Please enter a value from {min_val} to {max_val}")
            else:
                break
    return n


number = input_val(2, 100)
jeandemeusy
  • 226
  • 3
  • 12
  • And what for an empty string "" ? It still gives an error when I pass a condition for the same and seems to save the value from last iteration even after taking input. – Ishan Jindal Oct 21 '21 at 12:16
  • Just add a condition after the input. something like "if len(inp) == 0: print("error") return input_val()" – jeandemeusy Oct 21 '21 at 12:19
  • Recursion is not a good idea to solve this problem. – Karl Oct 21 '21 at 12:21
  • I know, but I kept his structure to make his code work. Personally I would have used a while loop with a flag to break out. – jeandemeusy Oct 21 '21 at 12:23