1

I would like to compare a string with a list, but my code isn't working

def compare(typeOfColors):
   introduceC = input("Introduce a color")
   while(introduceC.lower() != typeOfColors)
       print("Error")
colors = ["white", "black"] 
compare(colors)
2
  • 1
    Are you aware of the for statement? docs.python.org/3/tutorial/controlflow.html#for-statements Commented Dec 2, 2020 at 18:23
  • introduceC.lower() in typeOfColors is enough to know if the lower version of the input string is present in the list Commented Dec 2, 2020 at 18:24

3 Answers 3

1

To know if a string is in typeOfColors just use in

Note introduceC is unchanged in your loop, so if introduceC.lower() != typeOfColorsis false you loop without ending

To loop until the input is part of the list you can just do

def compare(typeOfColors):
   while (not input("Introduce a color: ").lower() in typeOfColors):
       print("Error")

compare(["white", "black"])

Example

Python 3.7.3 (default, Jul 25 2020, 13:03:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def compare(typeOfColors):
...    while (not input("Introduce a color: ").lower() in typeOfColors):
...        print("Error")
... 
>>> compare(["white", "black"])
Introduce a color: blue
Error
Introduce a color: red
Error
Introduce a color: bLack
>>> 
Sign up to request clarification or add additional context in comments.

Comments

1

Use the in keyword to test if an array contains a value.

def compare(typeOfColors):
   introduceC = input("Introduce a color")
   while introduceC.lower() not in typeOfColors:
       print("Error")
colors = ["white", "black"] 
compare(colors)

But that introduces a new error, infinite looping, which you could solve like this:

def compare(typeOfColors):
    introduceC = input("Introduce a color: ")
    if introduceC.lower() not in typeOfColors:
       print("Error")
       return compare(typeOfColors)

    print('Exists!')
    return introduceC

colors = ["white", "black"] 
compare(colors)

Giving:

Introduce a color: red
Error
Introduce a color: blue
Error
Introduce a color: whitE   
Exists!

If the user inputs something that is not in the accepted values, the function calls itself, effectively restarting. That is recursion.

4 Comments

introduceC never change => you loop without ending if the test is false
@bruno, I did mention as much, in any case I gave a more complete solution now.
Thanks, but i want the program to ask again for a color if its not in the list
@BRT88 It does ask again, try it!
0

Try looping through each element of the list. Then for each element comparing that with the color.

I'll try to help you if you need more than that.

3 Comments

there is no need top loop on the list, in will do that for you
But if i do a for loop, maybe the correct color is the last one, but i will have previous "Error" messages from the others
I don't really understand what you want then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.