2

Q is a user input, but when ever I type any colour it gives me blue no matter what. What am I doing wrong?

if q == "blue" or "Blue":
 color="33円[0;34m"
elif q == "cyan" or "Cyan" or "light blue" or "Light Blue" or "light Blue" or "Light blue":
 color="033円[1;36m"
elif q == "white" or "White":
 color="033円[0;37m" 
elif q == "green" or "Green":
 color="033円[0;32m"
elif q == "orange" or "Orange":
 color ="033円[0;33m"
elif q == "pink" or "Pink":
 color = "033円[1;31m"
AzyCrw4282
7,9145 gold badges26 silver badges43 bronze badges
asked May 11, 2020 at 1:29

4 Answers 4

2

Do this instead:

if (q == "blue") or (q == "Blue"):
 color="33円[0;34m"
elif (q == "cyan") or (q == "Cyan") or (q == "light blue") or (q == "Light Blue") or (q == "light Blue") or (q == "Light blue"):
 color="033円[1;36m"
elif (q == "white") or (q == "White"):
 color="033円[0;37m" 
elif (q == "green") or (q == "Green"):
 color="033円[0;32m"
elif (q == "orange") or (q == "Orange"):
 color ="033円[0;33m"
elif (q == "pink") or (q == "Pink"):
 color = "033円[1;31m"

As you can see the problem lies in how you laid out your conditions

What happened?

Your code processed if q == "blue" or "Blue" into if "Blue" which ultimately becomes true

You can test the former with this:

q == "blue" or "Blue" #prints "Blue"

and the latter with

if 'Blue':
 print("A") # The output
else:
 print("B")
answered May 11, 2020 at 1:37
Sign up to request clarification or add additional context in comments.

1 Comment

@AaurshA. You will have to post your code up for us to know whats up but sounds like a logical issue to me.
0

if q == "blue" or "Blue" returns true. if "Blue" will return true. You're looking for if q == "blue" or q == "Blue" or better, if q.lower() == "blue", or even better: if q.lower() in ['cyan', 'light blue']:

answered May 11, 2020 at 1:35

1 Comment

We've all made these sorts of oversights. :)
0

Actually the correct way of doing it is:

In [89]: if q in ["blue", "Blue"]:
 ...: color="33円[0;34m"
 ...: print(q)
 ...: elif q in ["cyan","Cyan","light blue","Light Blue","light Blue","Light blue"]:
 ...: color="033円[1;36m"
 ...: elif q in ["white","White"]:
 ...: color="033円[0;37m"
 ...: elif q in ["green","Green"]:
 ...: color="033円[0;32m"
 ...: elif q in ["orange", "Orange"]:
 ...: color ="033円[0;33m"
 ...: elif q in ["pink", "Pink"]:
 ...: color = "033円[1;31m"
 ...:

compare the elements from a list of values. In your case you are just comparing the q with one of the values.

answered May 11, 2020 at 1:37

Comments

0

whenever I type any colour it gives me blue no matter what

The reason is simple - Any non-None or non-zero value in python is a Falsy value and others are True.

So you have input q = "Red":

if q == "blue" or "Blue":, it evaluates to if False or True which further simplifies to if True. Thus the first condition is always satisfied. And hence the flaw in your code.

To solve the problem, rephrase your code to

if q in ["blue", "Blue"]:
 color="33円[0;34m"
elif q in ["cyan", "Cyan", "light blue", "Light Blue", "light Blue", "Light blue"]:
 color="033円[1;36m"
elif q in ["white", "White"]:
 color="033円[0;37m" 
elif q in ["green", "Green"]:
 color="033円[0;32m"
elif q in ["orange", "Orange"]:
 color ="033円[0;33m"
elif q in ["pink", "Pink"]:
 color = "033円[1;31m"

This would help.

Although a good attempt would be to write the code as follows:

q = q.lower()
if q == "blue":
 color="33円[0;34m"
elif q in ["cyan", "light blue"]:
 color="033円[1;36m"
elif q == "white":
 color="033円[0;37m" 
elif q == "green":
 color="033円[0;32m"
elif q == "orange":
 color ="033円[0;33m"
elif == "pink":
 color = "033円[1;31m"

Note: If you want to write less code and achieve more, you can drop the if-else construct and instead use dictionary as follows:

name2code = {"blue":"33円[0;34m", "white":"033円[0;37m", "light blue":"33円[0;36m", "cyan":"033円[0;36m","green":"33円[0;32m", "orange":"033円[0;33m","pink":"33円[0;31m"}
q_code = name2code[q.lower()]
answered May 11, 2020 at 1:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.