Home > Software engineering >  How do I access the parameters of the function one by one?
How do I access the parameters of the function one by one?

Time:07-21

So, I'm still learning how to code and I'm making this higher or lower game, and I made a function that pulls out info from a huge list with dictionaries inside it called "data":

Here is my code so far:

def choice_of_oponents(opponent1, opponent2):
  """
  Gets the info on 2 people randomly and assigns them to the variables 'opponent1' and 'opponent2'
  """
  opponent1 = random.choice(data)
  opponent2 = random.choice(data)

def winner(opponent1, opponent2):
 #This function is supposed to pull out the info stored in the parameters in the first function and determine the winner based on the data

I'm not sure how to write the second function or if it is even possible to pull out info from the parameters of the 1st function without making extra variables. Can someone help solve this?

CodePudding user response:

Maybe what you're missing is the concept of return-ing the output of a function. Also, you don't need any inputs to your first function (note that you didn't use them anyway).

Try something like this:

def choice_of_opponents():
  """
  Gets the info on 2 people randomly and assigns them to the variables 
'opponent1' and 'opponent2'
  """
  opponent1_result = random.choice(data)
  opponent2_result = random.choice(data)
  return opponent1_result, opponent2_result


def winner(opponent1, opponent2):
  if opponent1 > opponent2:
     return 1
  elif opponent2 > opponent1:
     return 2
  else:
    return 0  # A tie

op1, op2 = choice_of_opponents()
print(winner(op1, op2))
  • Related