1

I'm having some serious issues with getting the 'or' operator to work with strings in python 3.2

My code is similar to this:

   def choose_colour():

    cmd = input("Enter command here: ")

    if cmd != ("green") or ("blue") or ("yellow"):

        choose_colour()

    if cmd == ("green"):

        colour = ("g")

    if cmd == ("blue"):

        colour = ("b")

    if cmd == ("yellow"):

        colour = ("y")

    print (colour)

choose_colour()

However if I enter either one of these colours or not, it still repeats the 'choose_colour' function.

What am I doing wrong?

1 Answers1

1

If you try to use != for three condition you should apply != to all of them :

if cmd != ("green") or cmd != ("blue") or cmd != ("yellow"):

In Python we cant use != operator as you used in question . For example see here :

a = 1
b = 2
c = 1
if a == c or b:
    print ("ok")  # it will print ok how ever conditions a==b==c not satisfied

Because the conditions here checking is a == c and b==True not a==c and a==b.

ᴀʀᴍᴀɴ
  • 4,443
  • 8
  • 37
  • 57