1

This function is supposed to determine whether the user input is one of the viable (in this case, the strings "red", "yellow", and "blue" options, and if not ask for input again. However, upon typing in "yellow", it says NameError: name 'yellow' is not defined. Why does this happen?

    valid = False
    while not valid:
        init_choice = input("Give a primary color")
        if type(init_choice) == str:
            init_choice = init_choice.lower()
        if init_choice == "red" or init_choice == "yellow" or init_choice == "blue":
            valid = True
        if valid == False:
            print ("That is not a valid input.")
    return init_choice
3
  • It works fine for me as just one function. Commented May 4, 2020 at 2:11
  • 2
    Are you learning Python? If so why are you using Python 2? It hit end of life in January. BTW welcome to SO! Check out the tour and How to Ask. Commented May 4, 2020 at 2:12
  • @Tabulate -- That's because you are using python 3. Commented May 4, 2020 at 2:26

1 Answer 1

1

That happens because you are using input. Use raw_input instead. raw_input is what you have to use with python 2.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.