Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
does not exist
#1
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????
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  UndefinedEnvironmentName: 'extra' does not exist in evaluation environment EarthAndMoon 3 1,680 Oct-09-2023, 05:38 PM
Last Post: snippsat
  check if a file exist on the internet and get the size kucingkembar 6 1,758 Apr-16-2022, 05:09 PM
Last Post: kucingkembar
  SQLALCHEMY - Column doesn't exist jamesaarr 9 7,529 Nov-04-2021, 09:20 AM
Last Post: ndc85430
  pathlib destpath.exists() true even file does not exist NaN 9 4,653 Dec-01-2020, 12:43 PM
Last Post: NaN
  Syntax not exist noorpy 1 1,998 Nov-14-2019, 09:23 AM
Last Post: perfringo
  Shutil attempts to copy directories that don't exist ConsoleGeek 5 4,523 Oct-29-2019, 09:26 PM
Last Post: Gribouillis
  get value if it's exist [python] senait 1 1,755 Aug-21-2019, 06:47 AM
Last Post: buran
  Python says module doesn't exist... I say it does! EricMichel 2 3,078 May-14-2019, 03:26 AM
Last Post: EricMichel
  pathlib: resolving a path that does not exist Skaperen 6 5,475 Sep-08-2018, 12:25 AM
Last Post: Skaperen
  Does stack overflow exist in python ? sylas 3 3,792 Jul-23-2017, 09:21 AM
Last Post: sylas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020