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.

Code not working

+4 votes
asked May 3, 2020 by QuiiKy _ (460 points)
I am experimenting with colored text on python and can't find out why this isn't working:

import time
color = []

print("\x1b[1;32;40m Bright Green\n")
print("\x1b[1;31;40m Red\n")
print("\x1b[1;33;40m Yellow\n")
print("\x1b[1;34;40m Blue\n")
print("\x1b[1;35;40m Purple\n")
print("\x1b[1;36;40m Cyan\n")
print("\x1b[1;37;40m White\n")
print("\x1b[1;30;40m Black\n")
print('\x1b[0;37;40m Please enter disired text color\n')
if input() == 'bright green':
        print("\x1b[0;32;40m Great! You chose green!")
        color = ["Bright Green"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)
elif input() == 'red':
        print("\x1b[0;31;40m Great! You chose red!")
        color = ["Red"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)
elif input() == 'yellow':
        print("\x1b[0;33;40m Great! You chose yellow!")
        color = ["Yellow"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)
elif input() == 'blue':
        print("\x1b[0;34;40m Great! You chose blue!")
        color = ["Blue"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)  
elif input() == 'purple':
        print("\x1b[0;35;40m Great! You chose purple!")
        color = ["Purple"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)
elif input() == 'cyan':
        print("\x1b[0;36;40m Great! You chose cyan!")
        color = ["Cyan"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)
elif input() == 'black':
        print("\x1b[0;30;40m Great! You chose black!")
        color = ["Black"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)
elif input() == 'white':
        print("\x1b[0;37;40m Great! You chose white!\n")
        color = ["White"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)
else:
        print("Type lowercase or restart program.")

4 Answers

0 votes
answered May 5, 2020 by Birchi (150 points)
dude u dont have an input
+1 vote
answered Sep 29, 2020 by clebson henrique (160 points)

friend, you have to put an input (input name = input ('type something') in this case a variable remembering that the variable cannot be called "input" because errors can occur

0 votes
answered Jun 13, 2021 by ITSME (740 points)
import time
color = []

print("\x1b[1;32;40m Bright Green\n")
print("\x1b[1;31;40m Red\n")
print("\x1b[1;33;40m Yellow\n")
print("\x1b[1;34;40m Blue\n")
print("\x1b[1;35;40m Purple\n")
print("\x1b[1;36;40m Cyan\n")
print("\x1b[1;37;40m White\n")
print("\x1b[1;30;40m Black\n")
chosen = input('\x1b[0;37;40m Please enter disired text color\n')
chosen = chosen.lower()
if (chosen == 'bright green'):
        print("\x1b[0;32;40m Great! You chose green!")
        color = ["Bright Green"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)
elif (chosen == 'red'):
        print("\x1b[0;31;40m Great! You chose red!")
        color = ["Red"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)
elif (chosen == 'yellow'):
        print("\x1b[0;33;40m Great! You chose yellow!")
        color = ["Yellow"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)
elif (chosen == 'blue'):
        print("\x1b[0;34;40m Great! You chose blue!")
        color = ["Blue"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)  
elif (chosen == 'purple'):
        print("\x1b[0;35;40m Great! You chose purple!")
        color = ["Purple"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)
elif (chosen == 'cyan'):
        print("\x1b[0;36;40m Great! You chose cyan!")
        color = ["Cyan"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)
elif (chosen == 'black'):
        print("\x1b[0;30;40m Great! You chose black!")
        color = ["Black"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)
elif (chosen == 'white'):
        print("\x1b[0;37;40m Great! You chose white!\n")
        color = ["White"]
        time.sleep(2)
        if input('''You can see your current color by typing "color"
        ''') == 'color':
                print(color)
else:
        print("Invalid input or restart the program")
0 votes
answered Jun 21, 2021 by Laxmi Narsimha K (140 points)
The following is an optimized version of your work which in-turn aims at more user-friendly experience.

I hope this would be helpful.

Happy Coding ;)

import time
colors = {
    'bright green': '\x1b[1;32;40m',
    'red' : '\x1b[1;31;40m',
    'yellow' : '\x1b[1;33;40m',
    'blue' : '\x1b[1;34;40m',
    'purple' : '\x1b[1;35;40m',
    'cyan' : '\x1b[1;36;40m',
    'white' : '\x1b[1;37;40m',
    'black' : '\x1b[1;30;40m'
}

for item in colors.keys():
    print(colors.get(item), item);

while(True):
    print(colors.get('white'), 'Please enter disired text color\n')
    
    user_input = input().lower()
    
    desiredColor = colors.get(user_input);
    
    if(desiredColor is None):
        print("Sorry! Desired color is unavailable\nPlease choose from the provided ones...\n")
        continue
    print(desiredColor, 'Great! You chose', user_input)
    
    time.sleep(2)
    print("Want to give another try? : ( Anything apart from 'NO' is considered to be 'YES' )")
    if(input().lower()=='no'):
        print("Thank you for your Time!")
        break
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.
...