1

Hello new to Python here. I am having trouble adding a error check, where the input must be 1 to 20, and has to be an integer. If a letter is inputted or a number larger than 20 or less than 1 an error will show stating to try again.

        pick=input("pick 1-20: ")



        if pick == "1":
            apple()
        elif pick == "2":
            print("a")

        elif pick == "3":
            print("b")

        elif pick == "4":
            print("c")

        elif pick == "5":
            print("d")

        elif pick == "6":
            print("e")

        elif pick == "7":
            print("f")

        elif pick== "8":
            back()
IdriveEK9
  • 33
  • 1
  • a bit off topic but is this valid? elif ans > 8: ;how would I make it so that when ans is greater than a certain number print etc. – IdriveEK9 Nov 19 '19 at 08:13

1 Answers1

0

step1 : check whether integer.

use the isinstance(<var>, int)

ref: Checking whether a variable is an integer or not

example:

>>> a = 13
>>> b = 1.3
>>> c = "13"
>>> isinstance(a, int)
True
>>> isinstance(b, int)
False
>>> isinstance(c, int)
False

step2: check whether input number is between 1 to 20.

assuming 1 & 20 are inclusive:

>>> if ( a >= 1 and a <= 20):
...     print "YES" ## do your steps here. 
...
YES