Home > database >  How to use parameters/variables in a function outside of the function in a different block of code
How to use parameters/variables in a function outside of the function in a different block of code

Time:09-18

I am trying to assign value to the variables winsP1 and winsP2 by playing a random number game in my function 'determineWinner'. The values of winsP1 and winsP2 will be the number of times that each player wins the random number game.

The trouble I am running into is I am not able to pull the values from my function and use them outside of the function in my next block of code that tells me which player ultimately won the most rounds.

import random 
answer = input("Play the game?")

winsP1 = 0
winsP2 = 0

def determineWinner(winsP1, winsP2):
    from random import randint
    player1 = randint(1,10)
    player2 = randint(1,10)
    #time.sleep(1)
    print("Player 1:", player1)
    #time.sleep(1)
    print("Player 2:", player2)
    #time.sleep(1)
    if player1==player2:
        print("This round is a tie!")
        winsP1   1
        winsP2   1
    elif player1>player2:
        winsP1   1
        print("Player 1 wins this round!")
    elif player2>player1:
        print("Player 2 wins this round!")
        winsP2   1
    #time.sleep(1)

for i in range(10):
    determineWinner(winsP1, winsP2)

if answer == "y" or answer == "Y" or answer == "yes" or answer ==  "Yes":
    if winsP1>winsP2:
        print()
        print("The score totals are:")
        print("Player one: "   str(winsP1))
        print("Player two: "   str(winsP2))
        print()
        print("Player 1 wins with a score of", str(winsP1)    "!")
        print()
    elif winsP2>winsP1:
        print()
        print("The score totals are:")
        print("Player One: "   str(winsP1))
        print("Player two: "   str(winsP2))
        print()
        print("Player 2 wins with a score of", str(winsP2)    "!")
        print()
    elif winsP1==winsP2:
        print()
        print("The score totals are:")
        print("Player one: "   str(winsP1))
        print("Player two: "   str(winsP2))
        print()
        print("It's a tie!")
        print()

CodePudding user response:

Rather than iterating variables in place, consider using lists to collect results from determineWinner then subset to count by players:

from random import randint 
answer = input("Play the game?")

def determineWinner(): 
    player1 = randint(1,10) 
    player2 = randint(1,10)
 
    print("Player 1:", player1) 
    print("Player 2:", player2)
 
    if player1 == player2: 
        print("This round is a tie!") 
        result = 'T' 
    elif player1 > player2: 
        print("Player 1 wins this round!")
        result = 'P1'
    elif player2 > player1: 
        print("Player 2 wins this round!") 
        result = 'P2'

    return result

# BUILD LIST OF RESULTS VIA LIST COMPREHENSION 
results = [determineWinner() for i in range(10)]

# COUNT NUMBER OF ELEMENTS ACCORDING TO PLAYER
winsP1 = len([r for r in results if r in ("P1", "T")])
winsP2 = len([r for r in results if r in ("P2", "T")])

...

CodePudding user response:

winsP1 = 0
winsP2 = 0

def determineWinner(anyName1, anyName2):
    ...
    do stuff with anyName1 and anyName 2
    ...
    return anyName1, anyName2

winsP1, winsP2 = determineWinner(anyName1=winsP1, anyName2=winsP2)

The return keyword gives you back any variables you want from the function. Notice that I have returned more than one value in this case. The function can give back multiple values which are stored in a tuple. You can then unpack them like I have using variables seperated by a comma. Note that the function is using a copy of the variables you gave it, and when you get the values back, you have to overwrite the variables outside of the function with the returned copies. You can also simply write

winsValues = determineWinner(winsP1, winsP2) 

and then access them by writing

accessP1 = winValues[0]
accessP2 = winValues[1]
  • Related