Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

weird output

–1 vote
asked May 5, 2020 by QuiiKy _ (460 points) 1 flag
i am trying to make a simple color chooser:


print("\x1b[1;32;40m Bright Green\n")
print("\x1b[1;31;40m Red\n")
print('\x1b[0;37;40m Please enter disired text color\n')
if input() == 'bright green':
        print("\x1b[0;32;40m woo you chose bright green")
elif input() == 'red':
        print("\x1b[0;31;40m woo you chose red")


when i input red, i have to input it twice to get the result of the print().

when i input bright green, i only have to input if once to get the result

1 Answer

+2 votes
answered May 8, 2020 by Hunter (520 points)
selected May 13, 2020 by QuiiKy _
Best answer
It's because you're calling the input function from the if statement. Call input one time, store it in a variable, then compare that. Something like:

color = input()

if color == 'bright green':

elif color == 'red'

Basically what you're doing in the provided example is asking the user for input in the "if" line, comparing that to 'bright green' then asking for a separate input and comparing that to 'red'. As an example, try typing "red" then "bright green". You should get no output at all
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.