-4
votes
#Hex Check
def Check(HexInput):
    while HexInput!= Valid:
        print('That is an  invalid hex number.')
    else:
        print('That is an valid hex number.')
    return HexInput       

HexInput=input('Enter a hex number: ')
Valid='1234567890ABCDEFG'

When run, it outputs nothing when a hex number is entered. The program is aiming to process the users inputted Hex number and output whether it is a valid hex number or not.

3
you might want to actually run the function? BTW the "Valid" table is not valid as it goes up to G, and the check itself is incorrect too, assuming I got what you want to do. - Not_a_Golfer
Is that the entire program? You've defined Check(), but never called it. - g.d.d.c
Where would I call Check() (I'm a newbie to this stuff) - user1248367
"Damn you, computer! Do what I want, not what I tell you to!" - Justin ᚅᚔᚈᚄᚒᚔ
Hex digits are 0 through F, not 1 through G. - Jim Garrison

3 Answers

2
votes

Your program should probably look something more like this:

def Check(num):
  # checking code here.

n = raw_input('Enter a hex number:')

Check(n)

You need to call functions after you define them.

1
votes

using regex is much simpler in this case

#Hex Check

import re
def Check(HexInput):

    if re.match('^[a-f0-9]+$', HexInput, re.IGNORECASE):
        print('That is a valid hex number.')
    else:
        print('That is an invalid hex number.')


HexInput=input('Enter a hex number: ')
Check(HexInput)
1
votes

Try for a single character:

import string

def Check(HexInput):
    if HexInput not in string.hexdigits:
        print('That is an  invalid hex number.')
    else:
        print('That is an valid hex number.')
    return HexInput

string.hexdigits is builtin sequence of valid hexdigits. See the string module documentation for details.