Search code examples
pythondiceblackjack

Python: How do you make 2 values in two different functions equal?


So I'm trying to program a blackjack game in Python by using randomed dice and I was trying to compare the player function to the dealer function, but I don't know how to make one function compared to the other? If you could explain how I could make one local variable in a function equal to another local variable in a different function, that would be much appreciated. The code is for if you just want to see what I'm trying to do, it isn't completed.

A program to play Blackjack Dice.

import random
def roll_die():
return random.randint(1, 11)

def player():
    ''' 
    Implements what happens on player's turn. 
    Returns total and blackjack, which represents
    the player's total score and whether the
    player hit Blackjack, respectively.
    '''
    blackjack = False
    total = 0

    print('************ YOUR TURN ************')
    die1 = random.randint(1,11)
    die2 = random.randint(1,11)
    if die1 == 11 and die2 == 11:
        die1 = 10
    initial_roll = print('Roll: ',die1,die2)
    initial_total = die1+die2
    print('Total: ',initial_total)
    stay_or_roll = input('(s)tay or (r)oll? ')
    next_total = initial_total
    if next_total == 21:
        print(blackjack)

    while stay_or_roll == 'r' or next_total > 21:
        next_roll = (roll_die())
        print('\nRoll: ',next_roll)
        next_total = int(next_total+ next_roll)
        print('Total: ',next_total)
        if next_total > 21:
            print('Bust!')
        dealer()
        stay_or_roll = input('(s)tay or (r)oll? ')      
            if stay_or_roll == 's':
                dealer()     

    # < Insert the rest of your code here. >

def dealer():
    ''' 
    Implements what happens on the dealer's turn. 
    Returns total which represents the dealer's
    total score.
'''

print("\n********** DEALER'S TURN **********")
die1 = random.randint(1,11)
die2 = random.randint(1,11)
if die1 == 11 and die2 == 11:
    die1 = 10
initial_roll = print('Roll: ',die1,die2)
initial_total = die1+die2
print('Total: ',initial_total)
stay_or_roll = input('Press <enter to continue ...')
next_total = initial_total
if next_total >=16 or next_total <21:
    print('done')
if next_total == 21:
    print(Blackjack)
while stay_or_roll == '' and int(next_total) <= 21:
    next_roll = (roll_die())
    print('\nRoll: ',next_roll)
    next_total = int(next_total+ next_roll)
    print('Total: ',next_total)
    if next_total > 21:
        print('Bust!')     
    stay_or_roll = input('Press <enter to continue ...')       
def main():
    ''' 
    The main driver of the program. Connects the 
    player() and dealer() functions together to
    play Blackjack Dice.
    '''

    # The user (or player) plays first.
player_total, blackjack = player()
    print(player())
    print(dealer())
    # < Insert the rest of your code here. >

main()

Solution

  • You can't directly share local variables between functions. Your practical options are to return values, set global variables, pass in a mutable object to be modified to include the data, or rewrite to use a class that keeps the values as an attribute.