0

Hi I wrote a program which tells me if there is or not hexadecimals in the input.

hexadecimal = ['0','1','2','3','4','5','6','7','8','9','a','A','b','B','c','C','d','D','e','E','f','F']
output = ''
for c in hexadecimal:
  digit = input('Digit: ')
  output += c.join(digit)
  if digit == '':
    print(output, 'is a valid hexadecimal string.')
    break
  elif digit not in hexadecimal:
    print(digit, 'is not a valid hexadecimal digit.')
    break

The complete code of this program works, the only problem is that I need to add this into my code when the user doesn't enter anything.

enter image description here

cars
  • 318
  • 3
  • 11
  • 3
    Possible duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – dot.Py Mar 15 '17 at 12:54
  • 2
    What on earth is this supposed to be doing? `for c in hexadecimal`...`c.join(digit)`. This is very confused code. – khelwood Mar 15 '17 at 13:00
  • Are you just trying to write a script that takes input and tells you if its valid hex or not? – Hoopdady Mar 15 '17 at 13:06

4 Answers4

1

Use another if statement before appending it to output:

if len(output) == 0 and len(digit) == 0:
    print("input is blank")

This checks that the user hasn't previously entered anything and hasn't currently entered anything, if both are true then tell them the input is blank.

Andria
  • 4,712
  • 2
  • 22
  • 38
0

Modify your if:

if digit == '':
  if len(output)==0:
    print('The input was blank')
    break
  else:
    print(output, 'is a valid hexadecimal string.')
    break
elif digit not in hexadecimal:
  print(digit, 'is not a valid hexadecimal digit.')
  break
lyrjie
  • 141
  • 1
  • 5
0

Do it like this:

hexadecimal = ['0','1','2','3','4','5','6','7','8','9','a','A','b','B','c','C','d','D','e','E','f','F']
digit = input('Digit: ')

while digit is not '':
    if digit in hexadecimal:
        print(digit, 'is a valid hexadecimal string.')
    else:
        print(digit, 'is not a valid hexadecimal digit.')
    digit = input('Digit: ')

print('The input was blank')

It is better to use construction if digit in hexadecimal(it checks if provided input is in a given list) than doing for loop and inserting brakes.

0

Another way to deal with this, could be to use the string module and the built in functions all() and any().
all() returns True if all elements of the input are true (or if the input is empty).
any() returns True if any element of the input is a hexadecimal value.

string.hexdigits value is: '0123456789abcdefABCDEF'

Code:

import string

digit = input('Digit: ')

if not digit:
    print('The input was blank.')

elif all(c in string.hexdigits for c in digit):
    print(digit, 'is a valid hexadecimal string.')

elif any(c in string.hexdigits for c in digit):
    print(digit, 'there are hexadecimals in digit')

else:
    print(digit, 'is not a hexadecimal string.')
LaPriWa
  • 1,787
  • 1
  • 12
  • 19