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"
4 Answers 4
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")
1 Comment
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']:
1 Comment
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.
Comments
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()]