Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I access the parameters of the function one by one?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading