0

I have a function with 2 arguments. I would like to display the output using varying inputs of the arguments that can be found in two lists using a loop. I have executed the code successfully with one list (ages) but not with (ages) and (game).

# the below code works for 1 list assignment to the a argument  

for a in ages:
    print(age_price_event(a, 2))

# when I try this to assign the other argument I get an error.

for a in ages b in game:
    print(age_price_event(a, b))

File "", line 1 for a in ages b in game: ^ SyntaxError: invalid syntax

# Here is the function that I wrote

# Function that has two elements 
# a is the age of person
# b is the game
def age_price_event(a, b):
    str(b)
    if b == 1:            # looks to see if it is for 1
        if a < 4:              # if so execute code below
            return (0)    
        elif a < 18:
            return (10)
        else:
            return(15)     #Stop here
    if b==2:                     # looks to see if it is for 2
        if a < 4:                     # if so execute code below
            return (5)
        elif a < 18:
            return (55)
        else:
            return(66)                  #Stop Here
    else:               # if not game is not toady
        return ("That Game is not Today")

# here are the list to be assign to the arguments 
ages = [11, 22, 14, 25]
game = [ 1, 1, 2, 3]

# the below code works for 1 list assignment to the a argument  

for a in ages:
    print(age_price_event(a, 2))

# when I try this to assign the other argument I get an error.

for a in ages b in game:
    print(age_price_event(a, b))

the below code works for 1 list assignment to the a argument

55
66
55
66

when I try this to assign the other argument I get an error.

File "<ipython-input-158-2d5fb2f7d37f>", line 1
    for a in ages b in game:
                  ^
SyntaxError: invalid syntax
ComplicatedPhenomenon
  • 4,055
  • 2
  • 18
  • 45
in0
  • 57
  • 1
  • 2
  • 5

2 Answers2

1

This is called parallel iteration and can be written as follows:

for a, b in zip(ages, games):
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Python 3

for a, b in zip(ages, game):
    print(age_price_event(a, b))

Python 2

import itertools

for a, b in itertools.izip(ages, game):
    print(age_price_event(a, b))

In python 2 itertools.izip returns iterator instead of list, you want to use in Python 2 itertools.izip if you have so many elements.

Radek
  • 1,149
  • 2
  • 19
  • 40