简体   繁体   中英

Using functions and random to output two new items from a list. (Python 3.6)

I'm working on a Blackjack game written in Python (3.6). I'm doing this for an assignment in school as I'm beginning to learn this stuff. Basically, what I want to achieve is calling back on a function that uses random to deal out a new card. Obviously I want to make sure that the same card isn't dealt a second time (using a variable which has already been defined after randomly generating). What I've also worked for is creating two separate lists; one of values and one of suits, and so I want to have those combined. Once I have those combined, they're appended to a [black]list so that they will be excluded. Because each card is a combination of 2 things from seperate lists (values & suits), I need to assign a variable for both things combined, however, because I will need to be able to draw another card, I have opted to use a function instead of simply assigning a variable to each one, as I don't want to brute force this by having a bunch of variables which may or may not be used (if the user chooses to be 'hit' or 'stay'). The full code:

import random
J = 10
Q = 10
K = 10
A = 1
a = [1,2,3,4,5,6,7,8,9,10,J,Q,K,A]
b = ([('Spades'),('Clubs'),('Hearts'),('Diamonds')])
drawn = []
print ("Welcome to blackjack! The goal is to get to 21. If you go over you bust, and if you're under, whoever is closer to 21 to wins. Picture cards are worth 10 and an Ace is worth either 1 or 11.")
while True:
    x = random.choice(a)
    y = random.choice(b)
    m = random.choice(a)
    n = random.choice(b)
    h1 = (lambda x, y: x + y)
    h2 = (lambda m, n: m + n)
    drawn.append(h1 and h2)
    print ("You are dealt a " + str(x) + " of " + y + " and " + str (m) + " of " +  n)
    a1 = input ("What do you want to do? (Hit me/Stay)")
    if a1.lower() == "hit me":
        if h1 not in drawn:
            print ("You are dealt: ", h1)
            break
        else: continue
    if a1.lower() == "stay" or "hold":
        print ("You hold at ", str(x) + y, str(m) + n)

As you can see h1 is the function for adding both randoms x and y into one string. However it's a function and when it returns for

print ("You are dealt: ", h1)

it comes out with

You are dealt:  <function <lambda> at 0x04C73300>

(the number at the end changes every time as it's random).[Side Note: Changing the function to a classic def h1(): does not yield any changes except there is no <lambda> part.] I know the issue is due to me calling a function, but I don't know how to make this work without creating a bunch of defined variables. Also, I don't know if I can do this without breaking the loop that I rely on to get an output that isn't repeated, or without getting an infinite loop (done with:

if h1 not in drawn:
            print ("You are dealt: ", h1)
            break
        else: continue

I'm sure some of you great minds out there have a solution to my specific problem so any help is appreciated!

Thanks.

The h1 variable references a function. So when you print it, you're getting a string representation of the function itself, not the result of actually calling the function like you seem to want. It seems to me that the lambdas are useless for your use case so I would just do this:

#h1 = (lambda x, y: x + y)
#h2 = (lambda m, n: m + n)
h1 = x+y
h2 = m+n

I've figured it out with the help of a friend (far beyond anything this school could offer)!

I clearly had no idea what I was doing. I needed to redo the functions, make another function, and another list. The drawn list would deal with just the things dealt at the beginning and the new function would deal with anything being dealt as the result of requesting a 'hit'. It now looks like:

while True:
def rand_card():
    return (random.choice(a), random.choice(b))
drawn.append(rand_card())
drawn.append(rand_card())
def rand_hit():
    return (random.choice(a), random.choice(b))
hit.append(rand_card())

There was no need to add a and b because one is a meaningless string (the suit) and one is an int that holds value. Another list was added: hit[] which is added to via hit.append(rand_card()) . Now for the loop which checks to ensure I won't draw the same card twice, I've added:

 if a1.lower() == "hit me":
    if rand_card not in drawn and hit:
        print ("You are dealt: ", hit )
        print ("Your hand: ", drawn , hit)
        break
    else: continue

The final result is:

import random
J = 10
Q = 10
K = 10
A = 1
a = [1,2,3,4,5,6,7,8,9,10,J,Q,K,A]
b = ([('Spades'),('Clubs'),('Hearts'),('Diamonds')])
drawn = []
hit = []
print ("Welcome to blackjack! The goal is to get to 21. If you go over you bust, and if you're under, whoever is closer to 21 to wins. Picture cards are worth 10 and an Ace is worth either 1 or 11.")
while True:
    def rand_card():
        return (random.choice(a), random.choice(b))
    drawn.append(rand_card())
    drawn.append(rand_card())
    def rand_hit():
        return (random.choice(a), random.choice(b))
    hit.append(rand_card())
    print ("You are dealt a ", drawn)
    a1 = input ("What do you want to do? (Hit me/Stay)")
    if a1.lower() == "hit me":
        if rand_card not in drawn and hit:
            print ("You are dealt: ", hit )
            print ("Your hand: ", drawn , hit)
            break
        else: continue
    if a1.lower() == "stay" or "hold":
        print ("You hold at ", drawn)
        break

I'm still yet to implement the rest of the game (bust, potentially a dealer's hand, etc...) but for now it all works as intended!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM