In this code, I wanted to take 2 numbers and add them to the list to use later.
This code works correctly as long as it does not get an error, but when one of the exceptions is activated, the function returns None
.
class NotPositiveError(Exception):
pass
lenghts_list = []
def exception():
try:
user_num = int(input("Put a number: "))
if user_num > 0:
return user_num
else:
raise NotPositiveError
except ValueError as Err:
print(Err)
#return "False"
exception()
except NotPositiveError:
print("Sorry, your input should be positive")
#return "False"
exception()
print("put two number. The first is for the size of your main list and the second is for the size of the operator lists ")
for i in range(2):
user_lenght = exception()
print(user_lenght)
lenghts_list.append(user_lenght)
print(lenghts_list)
If you give it 1
and then 2
, it will give you [1, 2]
.
But if you give it "s"
, it will ask for input again and you give it 1
, it will go to the second one and you will go to 2
.
it will give you [None, 2]
How fix this!?