2

How do I set a value to only accept certain data in Python? Like I am making a code for a colour identifier. I want my variable to only accept up to FFFFFF any nothing greater than that. The base-16 characters pretty much...hex code.

The reason I am trying to do this is because if a user enters in a value like GGGGGG it will give them a Script Error, which actually makes me look incompetent (which I might be, but I do not want to look like I am). And also, if they enter in special characters like F1F2G% it will mess up too. In addition, if they leave the box blank, it also gives a Script Error.

I want to avoid those errors. Does anyone know of a good way?

shihongzhi
1,94116 silver badges17 bronze badges
asked Jun 15, 2012 at 1:08
4
  • How are you accepting input currently? What are you doing to validate your input currently? Commented Jun 15, 2012 at 1:12
  • Can you use a try/catch block? Commented Jun 15, 2012 at 1:13
  • Is the input a character string? Commented Jun 15, 2012 at 1:14
  • I am freely accepting input right now. If a user enters something, either it is the right information, or the Script just crashes. That is what I am trying to fix now. The one and only limiter is this: colour = form["colour"].value Commented Jun 15, 2012 at 1:33

3 Answers 3

11
try:
 val = int(hex_val, 16)
except ValueError:
 # Not a valid hex value
if val > int("FFFFFF", 16):
 # Value is too large
answered Jun 15, 2012 at 1:15
Sign up to request clarification or add additional context in comments.

9 Comments

This is the way to do it, also make the comparisons you want with the value and raise a manual exception if it's not right.
Because 0xFFFFFF is too mainstream?
Just for completeness, you should probably check to make sure val >=0
except ValueError: ^ SyntaxError: invalid syntax I do not know why I keep getting that?
@AjEntity -- The comments #Not a valid hex value and # Value is too large are for you to take some sort of action. You need to put code in there, but that code depends on how you want to handle the error.
|
1

You can also use the regex facility in re.

val = val.upper()
seeker = re.compile("^[0-9A-F]{1,6}$")
if seeker.search(val):
 hexCode = int(val, 16)
 # process a good value
else:
 #bail
answered Jun 15, 2012 at 1:27

1 Comment

seeker = re.compile([^0-9A-F]{1,6}$) ^ SyntaxError: invalid syntax That is the error I get from that?
1

This is one approach assuming the input is a string:

import string
def check_HEX(input):
 for l in input:
 if l not in string.hexdigits:
 return False
 return True

gives:

print check_HEX('FFFFFF') # True
print check_HEX('FFFZFF') # False
print check_HEX(' ') # False
print check_HEX('F1F2G%') # False
answered Jun 15, 2012 at 1:17

1 Comment

red_decimal = hexToInt (red) green_decimal = hexToInt (green) blue_decimal = hexToInt (blue) These values conflict with this because I have Int and not string. Sorry for not specifying.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.