mercredi 18 avril 2018

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.




Aucun commentaire:

Enregistrer un commentaire