Conditional structure if to correct

Hello ,

I changed the condition of if:

If len (choice)! = 1 and choice not in "12345" and not choice == "":

But this condition only tests if the double digits entry !

Text displayed:

  • | Your choice must be, between 1-5
  • | Try Again.

A space entry or character is not treated.

Here is the displayed text:

Try Again !

To have a good overview, here is the code used:

liste=[]
choice = ""
while choice not in "12345":
    print("------------------------------------")
    print("1: Add item to the list")
    print('2: Remove an item from the list')
    print("3: View the list")
    print("4: Clear the list")
    print("5: Quit")

    choice = input ("Your choice : ")

    if len(choice) != 1 and not in "12345" and not choice== " ":
        print("--| Your choice must be, between 1-5")
        print("--| Try again.") 

    elif choice== "1":
	...
    elif choice== "2":
	...
    elif choice== "3":
	...
    elif choice== "4":
	...
    elif choice== "5":
        print('Goodbye !')
        break
    else:
        print('Try again !')

Thank you in advance.

Because you used “and” - you have to use “or”.
Right now the check is only true if ALL 3 conditions are true - so if you enter “12”, despite being two digits, it wouldn’t be caught by the test because “12” is in “12345” → so it’s not even properly checking for two digits.

Hello Jayaya,

I may have worded the if condition incorrectly, but what I’m looking for is to check the options entered by the user, so that only numbers between 1 and 5 are accepted.

No “may have” - you did. You have 3 seperate conditions to check, meaning you have to use “or”, not “and”.

And if you want to check if a number has a specific value, then the easiest and most efficient way is to create a set() of possible ansers and if the answer is in the set. That’s just one condition.

If I wanna check if someone enters “banana” or “apple”, I’ll use if entry in set("banana", "soup") - not if entry in "bananasoup" because that would suddenly accept “ananas” as entry.

I modified the code, but even if I choose between “1” and “5” the following message will be displayed:

– | Your choice must be, between 1-5
– | Try Again.

liste=[]
choice = ""
while choice != "5":
    print("------------------------------------")
    print("1: Add item to the list")
    print('2: Remove an item from the list')
    print("3: View the list")
    print("4: Clear the list")
    print("5: Quit")

    choice = input ("Your choice : ")

    if len(choice) != 1 or choice not in ("1","2","3","4","5") or choice != " ":
        print("--| Your choice must be, between 1-5")
        print("--| Try again.") 

    elif choice== "1":
        print(1)
    elif choice== "2":
        print(2)
    elif choice== "3":
        print(3)
    elif choice== "4":
        print(4)
    elif choice== "5":
        print('Goodbye !')
        break
    else:
        print('Try again !')

The final condition is wrong.