In my code they're instructed to enter a width and height. However obviously these will be intergers, but I need to perform checks that they are intergers before they're used.

Atm I've got this:

while True:
        if width.isdigit() == True and height.isdigit() == True:
            if width >= 2 and width <= 8:
                if height >= 2 and height <= 8:
                    break
                else:
                    width, height = eval(input("Enter the width and height of patches: (e.g. 6, 3) "))
            else:
                width, height = eval(input("Enter the width and height of patches: (e.g. 6, 3) "))
        else:
            print("You have entered word(s) rather than numbers")

I remember reading in the Zelle book but can't find it about being able to "eval(width" or "eval(height)" or something after it's been in putted? Is this true?

Because I was thinking of using width, height = input("Enter the blah blah... ") rather than using eval. And then possibly I could perform the ".isdigit()" or ".isalpha()" function on it to check. Can someone shed some light please?

while True:
        width, height = eval(input("Enter the width and height of patches: (e.g. 6, 3) "))
        if str(width).isdigit() == False or str(height).isdigit() == False:
            print("Please enter numbers, not words")
        else:
            if width >= 2 and width <= 8:
                if height >= 2 and height <= 8:
                    break
                else:
                    width, height = eval(input("Enter the width and height of patches: (e.g. 6, 3) "))
            else:
                width, height = eval(input("Enter the width and height of patches: (e.g. 6, 3) "))

From reading what you gave me Tony, I came up with the above. It works, however, if the user does enter a string and not an int, the in-putted string would have to be in quotation marks. Which would not always be the case...

Edited 4 Years Ago by AdampskiB: n/a

You did not use try..except. Eval is unsafe, you can make the computer format your hard disk for example, if you know how. That is why the user input should be checked for correctness. Now you are taking tuple of values, but you should refactor that out of the checking function and input with correct range one by one announcing the user the acceptable range.

Edited 4 Years Ago by pyTony: n/a

Ok I appreciate eval in unsafe, however, we haven't been introduced what you recommended in our worksheets this semester so I don't think lecturers would deem it as acceptable to use in my program.

while True:
        width, height = eval(input("Enter the width and height of patches: (e.g. 6, 3) "))
        if str(width).isalpha() == True or str(height).isalpha() == True:
            print("Please enter numbers, not words")
        else:
            if width >= 2 and width <= 8:
                if height >= 2 and height <= 8:
                    break
                else:
                    width, height = eval(input("Enter the width and height of patches: (e.g. 6, 3) "))
            else:
                width, height = eval(input("Enter the width and height of patches: (e.g. 6, 3) "))

The above code for-fills the purpose, however it only works when a non-int input has been entered when it's been entered with quotation marks. How do I go about the error message being displayed when a non-int input has been entered without quotation marks?

E.g. if you entered "blah" or "asjdkasjd" in the input field, it would produce message, but if something has been entered which is not an number, without quotation marks it would just crash with the python error, of

NameError: name 'whateverisentered' is not defined

Edited 4 Years Ago by AdampskiB: n/a

try:
     # processing possibly causing exception
except NameError:
     print('You must input strings like this: "my string"')
else:
     # code after possible exception point

Like I said, I don't think I'd be able to use what you recommended.

Again, I haven't used "try" or "except" in previous worksheets nor have I seen it in the book given to us to use for the unit so I wouldn't be happy using those functions. Is there literally no way around it then?

You can code your own check using Python's isdigit() function, but as we have no way of knowing what you have studied in class, you will probably just come back and say that you haven't used it in class, so it would be a further waste of time.

def int_check(str_in):
    for ch in str_in:
        if ch < "0" or ch > "9":
            return False, str_in
    return True, int(str_in)

for test_input in ["123", "AString", "123.45"]:
    ret, num = int_check(test_input)
    if ret:
        print num, "IS an integer"
    else:
        print num, "is NOT an integer"

Note that this is the only code that I will post. If you come back and say "I don't like that" then I will say that you are on your own and will have to provide some code/effort yourself as your original question did not state any limitations, so don't expect us to keep coming back with option after option like a performing seal until you find one that you like,

Edited 4 Years Ago by woooee: n/a

That was a pretty rude reply, none the less I am grateful for all the time effort you both have put in to helping me. I was just stating that we haven't used those functions within class, and tony seemed to of ignored it 3 times and carried on suggesting using "try" and "except".

I won't waist any more of your time, though I did state in the 5th post what tony suggested wasn't ideal for me.

Correct, woooeee. Only you wasted your effort in obfuscating, as OP knows is_digit() allready and is using it. He is only hitting himself to feet trying to check tuples not individual values from user. Here my version, as OP probably has not learned tertiary if also :icon_evil:

for test_input in ["123", "AString", "123.45"]:
        print test_input, "IS an integer" if test_input.isdigit() else "is NOT an integer"

OP: the problem is not Python expressions, it is that you are not testing user inputs one by one. I took you not wanting to use int() function, but eval, not about try..except which you should use for robust code in any case.

Edited 4 Years Ago by pyTony: n/a

This question has already been answered. Start a new discussion instead.