Python Forum

Full Version: does not exist
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I had this code:
    import color
    Block_Color_Q = input('Robot Color: Red(1) Blue(2) Green(3) Yellow(4) Orange(5)')
    if Block_Color_Q == 1:
        Block_Color = color.red
    elif Block_Color_Q == 2:
        Block_Color = color.med_light_blue
    elif Block_Color_Q == 3:
        Block_Color=color.lime
    elif Block_Color_Q == 4:
        Block_Color = color.dandilion_yellow
    elif Block_Color_Q == 5:
        Block_Color = color.orange
Later in my code I had:
print(Block Color)
What SHOULD happen is if they entered 3, than it would print(color.lime) but only half the time it works, the other half an error pops up saying
Error:
Block_Color does not exist
Why????
If you run this in Python 3.x, you will always get that error. In Python 3.x, input returns a string.

In Python 2.x, this should work if the user types the correct thing in. However, if they don't, you'll get that error.

I would add a print(repr(Block_Color_Q)) to the program, to show you exactly what the user entered and how the program interpreted it.
(Feb-11-2017, 12:12 AM)ichabod801 Wrote: [ -> ]If you run this in Python 3.x, you will always get that error. In Python 3.x, input returns a string.

Ya so according to ichabod801 if you are using python 3.x you might what to do something like this

x = int(input("Something"))
Here x will be an int. Because you are testing for an int you code will probably work