Home > Mobile >  how to call a function to run on different data sets determined by user input in python?
how to call a function to run on different data sets determined by user input in python?

Time:06-10

I have routes (route001 and route002). The user will select either one. I then need the code to recognise what route has been selected and change the start and directions variables accordingly (in the code below start and directions has the data for route001). I then want to run the function called vectors() on the route data that was selected.

I am unsure of how to change start and directions depending on what route the user selected. I am also unsure how to call the function vectors() on the route data. I am not confident either that I have created the function correctly.

Any help would be much appreciated as I am very new to this.

  route001 = (3, 12, 'S', 'S', 'W', 'S', 'S', 'S', 'E', 'E', 'E', 'S', 'S', 'W',
            'W', 'S', 'E', 'E', 'E', 'E', 'N', 'N', 'N', 'N', 'W', 'N', 'N',
            'E', 'E', 'S', 'E', 'S', 'E', 'S', 'S', 'W', 'S', 'S', 'S', 'S',
            'S', 'E', 'N', 'E', 'E')

  route002 = (12, 11, 'W', 'W', 'S', 'S', 'S', 'W', 'W', 'N', 'N', 'N', 'W', 'W', 
        'W', 'S', 'S', 'S', 'S', 'E', 'E', 'S', 'W', 'W', 'W', 'W', 'N', 'N', 
        'W', 'W', 'S', 'S', 'S', 'S', 'E', 'E', 'E', 'S', 'E', 'S', 'E', 'S')

  start = [route001[0]]   [route001[1]]
  directions = route001[2:]

  coordinates = {"N": [0, 1], 'E': [1, 0], 'S': [0, -1], 'W': [-1, 0]}


  def vectors():

  for d in directions:
    dx, dy = coordinates[d]
    start[0]  = dx
    start[1]  = dy
    if start[0] < 0 or start[0] > 12:
        print('Error: This route goes outside the grid')
        break
    elif start[1] < 0 or start[1] > 12:
        print('Error: This route goes outside the grid')
        break
    else:
        print(start)   

CodePudding user response:

First, let the user input 1 or 2:

routeSelection = input("Press 1 or 2 to select a route")

Then asign start and directions accordingly:

if routeSelection == "1":
    selectedRoute = route001
else if routeSelection == "2":
    selectedRoute = route002
else #error

start = [selectedRoute [0]]   [selectedRoute [1]]
directions = selectedRoute [2:]

To then call the vectors function just put

vectors()

(Since the vector function uses the global start and directions, you dont need to pass any argument.)

OR:

You could also define the vectors() to take arguments and then call it by passing start and directions as arguments. To avoid confusion, you might want to rename the parameters inside the vectors function to something else:

# call of vectors
vectors(start, directions)

def vectors(startPoint, directionsList):
  for d in directionsList:
    dx, dy = coordinates[d]
    startPoint[0]  = dx
    startPoint[1]  = dy
    ...

If you decide to go this way AND you dont need the start and directions variable in the global scope, you could even directly call vectors() with the according arguments without assigning start and directions at all:

vectors([selectedRoute [0]]   [selectedRoute [1]], selectedRoute [2:])
  • Related