0

new to python and trying to think up how to approach having a list of valid characters allowed for a key within my dictionary.This key can be any combination of the characters all the way down to a single character or empty.

For example:

allowedWalkingDirection['N','n,'S','s','E','e','W','w']
def isRotateValid(path):
   if allowedWalkingDirection in path['walk']:
       return path
   return False

And so if I try say: {'rotate':'WeNsE'} my input says it isn't valid. I'm sorry if this isn't very clear and concise, in short, my goal is to allow the valid walking directions to be input however many times within my key, but it's currently only allowing one character in the string.

  • 1
    I don't totally understand what you want to do, but a simple check would be : `all(c.lower() in allowed for c in path)`. This checks that all characters in the string `path` are in the list `allowed`. Also, the list of allowed path doesn't need duplicates for uppercase and lowercase since `c.lower()` converts each character to a lowercase. – Mateo Vial Feb 25 '22 at 01:20
  • The case matters, not for this specific case but for what I'm actually trying to do, it matters.(rotating a cube) So in my actual code the uppercase represent clockwise rotation and the lowercase represents counter-clockwise. @MateoVial – waterygatorade Feb 25 '22 at 01:28
  • 1
    Oh, all right. So did you try what I suggested ? – Mateo Vial Feb 25 '22 at 01:31
  • Are you saying I should call the all() as a for loop within my bool check method? I'm sorry I'm a bit confused about where you'd suggest I place it! @MateoVial – waterygatorade Feb 25 '22 at 01:39

1 Answers1

0

ok, upon further brain melting and relentless internet perusing I've found some help from Valid characters in a String , I then thought of implementing something like

def isRotateValid(path):

for i in range(0,(len(path['walk']))):
    if path['walk'][i] not in allowedWalkingDirection:
        return False