-4

In this program 10 inputs are taken into a list and now need to add a condition that is the input data should be between -20 and +20 and if the value is anything beyond the condition an exception error is to be shown. How do i do it ?

CODE:
lst=[]
print("Enter 10 numbers : \n")
for i in range(0,10):
    lst1=int(input())
    lst.append(lst1)

print("\nThe entered list is : ",lst)

1 Answers1

1

You can test the value of lst1, then raise an exception if it falls outside your valid range:

for i in range(0,10):
    lst1=int(input())
    if lst1 < -20 or lst1 > 20:
        raise ValueError("Out-of-range value entered")
    lst.append(lst1)

Sample output:

1
2
-30
Traceback (most recent call last):
  File "./temp.py", line 8, in <module>
    raise ValueError("Out-of-range value entered")
ValueError: Out-of-range value entered
dspencer
  • 4,297
  • 4
  • 22
  • 43