Python program to Get Index Out of Exception

x = [1,2,3,4,5,6,7,8,9]
print("t = terminate")
while True:
 num = input("Choose index to search: ")
 if num == 't':
     break
 try:
     num = int(num)
     print(x[num])
 except ValueError:
    print("Only integers are allowed!")
 except IndexError:
    print("Error! Number out of index", num)

 

 

 

Output:

t = terminate
Choose index to search: 4
5
Choose index to search: 11
Error! Number out of index 11
Choose index to search:

 

Subscribe For Daily Updates

100+ Python Pattern Examplespython pattern examples - star patterns, number patterns