I've been searching on here for a bit and nothing seems to really encapsulate my actual issue, being that I want my given string(if its characters fit my criteria) to cause different if statements. The purpose of my code is for each letter to represent a rotation of my string.
example parts['rotate'] = 'Ff'
allowed = ['F', 'f', 'R', 'r', 'B', 'b', 'L', 'l', 'U', 'u', 'D', 'd']
def rotated(parms):
result = {}
if ('cube' not in parms):
result['status'] = 'error: cube is missing!'
cube = list(parms['cube'])
if ('rotate' not in parms or parms['rotate'] == ''):
return rotateF(cube)
if (parms['rotate'] == 'F'):
return rotateF(cube)
if (parms['rotate'] == 'f'):
return rotatef(cube)
if (parms['rotate'] != allowed):
result['status'] = 'error: invalid rotation'
return result
def rotateF(cube):
rotatedCube = list(cube)
.....
rotatedCube = ''.join(rotatedCube)
return rotatedCube
def rotatef(cube):
rotatedCube = list(cube)
......
rotatedCube = ''.join(rotatedCube)
return rotatedCube
And so currently, the return i'm getting is for 'rotate' = 'Ff' :
{'status': 'error: invalid rotation'}
My assumption is that it is because currently, my code only allows for one character at a time when I just wanted to ensure that those were the only allowed characters, not one per string.
CodePudding user response:
The way your rotate function is written is to compare against exactly a specific string, but it seems like you are providing a list of rotations (in python, a string is a list of characters). Try introducing another helper function, splitting rotated
in to something like do_single_rotation
, and do_all_rotations
(I'm leaving out some of the error handling and whatnot as I'm not sure exactly what your goal is for the function to return)
default_rotate_str = 'F'
def do_all_rotations(parms):
rotate_str = parms.get('rotate', default_rotate_str)
cube = list(parms['cube'])
for rotate_command in rotate_str:
cube = do_single_rotation(cube, rotate_command)
return cube
def do_single_rotation(cube, rotate_command):
if rotate_command == 'F':
return rotate_F(cube)
if rotate_command == 'f';
return rotate_f(cube)
# .... continue
if rotate_command == 'D':
rotate_d(cube)
raise ValueError("unrecognized rotate command")