weird output

–2 votes
asked May 5 by QuiiKy _ (410 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

+1 vote
answered May 8 by Hunter (500 points)
selected May 13 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.
...