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.

*** NameError: name 'red' is not defined

0 votes
asked Jan 2, 2023 by sudheer kumar (120 points)
import random

colors = ["white" , "black" , "red" , "green" , "blue" , "yellow" , "purple" , "grey"]

while True:
        color = colors[random.randint(0,len(colors)-1)]
        guess = input("I'm thinking about a color, can you guess it: ")

while True:
        if (color == guess.lower()):
                break
        else:
                guess = input("Nope. Try again: ")

        print("You guessed it! I was thinking about", color)

        play_again = input("Lets pllay again? Type 'no' to quit.")

        if play_again.lower() == 'no':
                break

        print("It was fun playing!")

2 Answers

0 votes
answered Jan 2, 2023 by Peter Minarik (88,130 points)

***NameError: name 'red' is not defined

The code you shared has no such error.

However, it has wrong indentations.

I've fixed the indentation so it should work right now:

import random

colors = ["white" , "black" , "red" , "green" , "blue" , "yellow" , "purple" , "grey"]

while True:
    color = colors[random.randint(0,len(colors)-1)]
    guess = input("I'm thinking about a color, can you guess it: ")

    while True:
        if (color == guess.lower()):
            break
        else:
            guess = input("Nope. Try again: ")

    print("You guessed it! I was thinking about", color)
    play_again = input("Lets pllay again? Type 'no' to quit.")
    if play_again.lower() == 'no':
        break

print("It was fun playing!")
+1 vote
answered Jan 5, 2023 by Kashif Agha (160 points)
No error within the code, however, there is a multiple Indentation errors. fix the indents. Also, I suggest adding the colors you can guess from so the user knows what colours there are.
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.
...