How Can I Determine If User Input Is A Valid Hexadecimal Number?
Solution 1:
Change line 6 to
Hex=int(input('Enter a hex number: '), 16)
This line would successfully parse any hexadecimal input (for example, '0x123f') and would throw a ValueError on an invalid input (such as 'hello').
ETA: Based on your comments, the following is all you need:
user_input = input('Enter a hex number: ')
try:
hexval = int(user_input, 16)
print'That is a valid hex value.'except:
print'That is an invalid hex value.'
ETA: If you really have to have a Check
function, this structure would be the best way to do it:
import re
defCheck(s):
"""Check if a string is a valid hexadecimal number"""# code for checking if it is a valid hex number here
user_input = raw_input("Enter a hex number: ")
if Check(user_input):
print'That is a valid hex value.'else:
print'That is an invalid hex value.'
Since this is a homework question I'm not going to finish the answer- just know that the Check
function has to return True if the string is a valid hex statement or False if the string is not.
There are many ideas among everyone's answers of how to do it, and you could indeed use a try/except statement like I do above. One of the best ways to do it would be to use regular expressions, which are a very powerful way to parse strings.
Solution 2:
Investigate the regex library. Or do explicit cast to int then catch any errors and process them accordingly.
Solution 3:
The only valid values for hex strings are 0-9, A-F
. It should be possible to store these values in a list / array and then do a simple contains call. Something along the lines of this:
forcharin userInput:
ifnotcharin validTokens:
print'invalid hex value'
Solution 4:
Here is a nice, Python way to check your input for a valid hex number.
defCheck():
whilenot Valid:
try:
Hex=int(raw_input('Enter a hex number: '), 16)
print('That is valid.')
returnTrueexcept ValueError:
print('That is an invalid entry.')
returnFalse
Solution 5:
I am answering this question from a Python philosophy point of view, because you have already received good answers, some of them involving try: .. except
.
The Pythonic thing about using exception handling try: .. except
is the way Python programmers seem to be encouraged to use it is, at least for me, a departure from exception handling in other languages.
In Python, you are encouraged to raise an exception, either explicitly using raise or more implicitly within the construct of try .. except.
When I posed a question a while back about how to deal with null integer values in a .csv file, I was encouraged to go ahead with the assignment to the Python integer variable within try: .. except, instead of testing first to test to see if the value was null.
The answer went on to say don't bother to test and then take action, use exception handling, because it is more Pythonic. Using try: .. except also appeared to consume fewer instructions.
That attitude got me to write more exception handling than I would have thinking using try: .. except was reserved only for when bad things happen.
Post a Comment for "How Can I Determine If User Input Is A Valid Hexadecimal Number?"