-3

So, I have this code in python but I want it to display a message if the string entered from user is not from "a" to "j" (case insensitive). So the user is not allowed to enter strings that are not in this range. How can I do that?

I have tried the codes below but it won't work.

  words = "Hello world ace"
  words = [word.lower() for word in words.split()]
  for word in words:
      print(sorted(word) == list(word))
xhg
  • 1,850
  • 2
  • 21
  • 35
Gizele
  • 9
  • 8
  • 1
    It's not clear what you mean - that code doesn't currently take any input from the user, or attempt to validate it. Have a look at http://stackoverflow.com/q/23294658/3001761 for general patterns on input validation. – jonrsharpe Mar 26 '17 at 16:13
  • Tell me if I understood what you want to do. You want to allow the user just to input any combination of the letters a, b, c, d, e, f, g, h, i and j? – Juan T Mar 26 '17 at 16:16
  • It's not clear what you mean by "if the string entered is not from (a To j)", either. – user2357112 Mar 26 '17 at 16:17
  • @JuanT yes i want it like that – Gizele Mar 26 '17 at 16:20
  • @user2357112 yes that is exaclly what i mean – Gizele Mar 26 '17 at 16:21

1 Answers1

0

(Assume space is also allowed)

Check this:

words = "Hello world ace"
allowed = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', ' ']
if not all(character in allowed for character in words.lower()):
    print("!!!")
xhg
  • 1,850
  • 2
  • 21
  • 35