-1

I am new to python.

The following code should read an integer into the voting rating:

`rating = input('Enter an integer rating between 1 and 10')`

My doubt: The problem with the above code is it allows any values without error. How can I insert error message?

Alexandre B.
  • 5,387
  • 2
  • 17
  • 40
  • 1
    `input()` always returns string and it never check if it is integer, float or string. You have to check it on your own. First you can convert string to integer `value = int(rating)` to check if it is integer and later check range with `1 <= value <= 10` – furas May 12 '19 at 13:46
  • @furas `input()` always returns a string in python 3. In python 2 it tries to `eval()` the expression passed to it. If a user wants a string in python 2 then they need to use `raw_input()` – Matthew Barlowe May 12 '19 at 14:11
  • You can try to parse str to int and then decide accordingly @user11052359 – Devesh Kumar Singh May 12 '19 at 14:36
  • @MatthewBarlowe I know this but I don't use Python 2 almost 4 or 5 years. Is anybody use it yet? – furas May 12 '19 at 15:27

2 Answers2

0

You can try to parse the string to an integer, and if you cannot, print accordingly, but if you can and the integer is between 1 and 10, decide accordingly

def check_int(s):

    is_int = False
    try:
        int(s)
        is_int = True
    except:
        pass

    return is_int

rating = input('Enter an integer rating between 1 and 10>>')
#If string can be converted to integer
if check_int(rating):
    #Convert it to an integer and compare ranges
    r = int(rating)
    if 1<=r<=10:
        print('Integer is', r)
    else:
        print('Integer is not between 1 and 10')
#Else print error
else:
    print('Not an integer')

The output will be

Enter an integer rating between 1 and 10>>11
Integer is not between 1 and 10

Enter an integer rating between 1 and 10>>6
Integer is 6

Enter an integer rating between 1 and 10>>hello
Not an integer
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
0

You use a function like this: While the imput isn't correct, we ask a new input. We also check the input is an number with cast. If it's not a number, that will raise an exception that we catch in the try ... catch.

def getInputVal():
    # Boolean equal to false while the input isn't correct
    correct_answer = False
    while (not correct_answer):

        # Read the input (string)
        val = input('Enter an integer rating between 1 and 10: ')

        try:                                        # Try to cast the string as an integer
            val_int = int(val)
            if (val_int >= 1 and val_int <= 10):    # If the value is in the right interval
                correct_answer = True               # We go out of the loop
                print("Well done, your value is: ", val_int)           # We display the value
        except:                                     # If the cast raise an error
            print("A number is expected")           # An "error" message is shwon
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40