I have written the below code to know the mood of the user. the code runs successfully except for the data input validation function. In my case if the user enters the value above 3 the code accept it and enters the value as none in the array. whereas i need the code to validate and tell that the input entered is wrong and please enter the correct value.
please help me with input validation.
The code is below
def let_user_pick(options):
print("Please choose:")
for idx, element in enumerate(options):
print("{}) {}".format(idx+1,element))
i = input("Enter number: ")
try:
if 0 < int(i) <= len(options):
return int(i)
except:
pass
return None
def _sum(arr):
sum=0
for i in arr: sum = sum + i
return(sum)
color=["Red","orange","green"]
Animal=["Dog","lizard","Donkey"]
Food=["Ice-cream","Vegetables","Exotic-food"]
Book=["the kite runner","Winter Garden","White Trash"]
Smell=["Floral","Chemical","Sweet"]
a=[]
print("Input the color from the list")
a.append(let_user_pick(color))
print("Input the Animal from the list")
a.append(let_user_pick(Animal))
print("Input the Food from the list")
a.append(let_user_pick(Food))
print("Input the Book from the list")
a.append(let_user_pick(Book))
print("Input the Smell from the list")
a.append(let_user_pick(Smell))
print(a)
ans = _sum(a)
print(ans)
if (ans == 15 and ans >= 13) :
print("User is anxious")
elif (ans == 12 and ans >= 10):
print("user is Happy")
elif (ans == 9 and ans >= 6) :
print("user is Neutral ")
elif (ans == 5 and ans >=4) :
print("user is Sad")
else:
print("user is angry")