2

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")
  • no shiv, I need if someone enters any number above 3 in my case it must prompt user that the value is incorrect and input the correct value – francis fernandes Feb 07 '22 at 17:01
  • `let_user_pick` should repeat the question until they enter a number, rather than returning `None`. – Barmar Feb 07 '22 at 17:12
  • By the way, you have too much wrapped up in the try statement. I'm assuming it's meant to catch non-integer strings a user might put in, but you should just put a variable assignment in the try and not make the scope too big. – Andrew Holmgren Feb 07 '22 at 17:21

1 Answers1

2

The problem is that you don't check the output of the function whether it is None or a number you can do this:

user_choice = let_user_pick(<array>)
if user_choice is None:
    print("Please Enter a Number below 3")
    let_user_pick(<array>)
else:
    a.append(user_choice)

You can develop your code by making it a while loop until all the answers are correct then break the loop and continue what you were doing.

Edit: it looks like my code didn't seem to work with you, so I edited it with a tested code, here it is:

print("Input the color from the list")
while True:
    user_input = let_user_pick(color)
    if user_input is None:
        print("Please choose a number below 4")
        data = let_user_pick(color)
    else:
        a.append(user_input)
        break

and yeah absolutely you can apply this to the others, or you can combine them all and make it one while loop

CTRLDON
  • 73
  • 2
  • 7
  • 1
    CTRLDON, tried your solution didnt seem to work for me , if the value entered at the first time is wrong it prompts to re-enter the value again but once the correct value is entered on second attempt it fails to append the value to the array , array remains blank – francis fernandes Feb 08 '22 at 10:13
  • I edited it , check it out – CTRLDON Feb 08 '22 at 15:10