'My if statement for user input keeps getting skipped [duplicate]
I am trying to create a code that takes in user input and prints it out in a different color of the user's choice. When I run the code, the if statement for the color choices gets skipped. Here is the code:
from termcolor import colored
while True:
try:
print("Colors = grey, red, green, yellow, blue, magenta, cyan, white")
Colors = ['grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']
print(colored(input("Type Something: "), input("Enter a Color: ")))
except ValueError:
print("Not A Valid Color")
continue
else:
break
while True:
if input("Enter a Color: ") == Colors:
print("Not a Valid Color")
else:
break
Does anyone know how to fix it?
Solution 1:[1]
I believe what you are trying to check is whether the given input by the user is inside the Colors list.
In that case, your condition is wrong - input returns a string, and you are checking whether it is equal to a list. Instead, you want to check if the given input is inside the list or not :
if input("Enter a Color: ") not in Colors:
print("Not a Valid Color")
else:
....
Solution 2:[2]
you can do it using :
ax.text()
check this site example which use it with for loop , just like your data .
https://www.tutorialspoint.com/how-to-write-text-above-the-bars-on-a-bar-plot-python-matplotlib
Solution 3:[3]
Since you didn't provide a code sample try to adjust this with your variables it will work as you asked :
x = np.arange(len(years)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
ax.set_ylabel('Population(in million)')
ax.set_title('Years')
ax.set_xticks(x)
ax.set_xticklabels(years)
pps = ax.bar(x, population, width, label='population')
for p in pps:
height = p.get_height()
ax.text(x=p.get_x() + p.get_width() / 2, y=height/2.5,
s="{}".format(height),
ha='center',
rotation=90
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Liron Berger |
| Solution 2 | Mr Dream |
| Solution 3 | Mr Dream |

