使用函数和随机从列表中输出两个新项目。 (Python 3.6)

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

我正在开发用 Python (3.6) 编写的二十一点游戏。当我开始学习这些东西时,我正在为学校的作业做这件事。基本上,我想要实现的是回调一个使用 random 来发新牌的函数。显然,我想确保不会再次发同一张牌(使用随机生成后已经定义的变量)。我还致力于创建两个单独的列表;一种价值观和一种西装,所以我想把它们结合起来。一旦我将它们组合在一起,它们就会被添加到 [黑] 名单中,这样它们就会被排除在外。因为每张牌都是来自不同列表(价值和花色)的 2 种东西的组合,所以我需要为这两种东西组合分配一个变量,但是,因为我需要能够绘制另一张牌,所以我选择使用函数而不是简单地为每个变量分配一个变量,因为我不想通过一堆可能会或可能不会使用的变量来强制执行此操作(如果用户选择 'hit' 或 'stay').完整代码:

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)

如您所见,h1 是将随机数 xy 添加到一个字符串中的函数。然而,它是一个函数,当它 returns for

print ("You are dealt: ", h1)

结果是

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

(末尾的数字每次都会变化,因为它是随机的)。[旁注:将函数更改为经典 def h1(): 不会产生任何变化,除了没有 <lambda> 部分。 ] 我知道这个问题是由于我调用了一个函数,但我不知道如何在不创建一堆已定义变量的情况下使它工作。另外,我不知道我是否可以在不破坏我依赖的循环来获得不重复的输出,或者没有无限循环的情况下做到这一点(完成:

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

我敢肯定你们中的某些人有解决我的具体问题的方法,因此非常感谢您的帮助!

谢谢。

h1 变量引用了一个函数。所以当你打印它时,你得到的是函数本身的字符串表示,而不是像你想要的那样实际调用函数的结果。在我看来,lambda 表达式对您的用例毫无用处,所以我会这样做:

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

我在朋友的帮助下弄明白了(这远远超出了这所学校所能提供的)!

我显然不知道自己在做什么。我需要重做这些功能,制作另一个功能和另一个列表。绘制的列表将只处理开始时处理的事情,而新函数将处理作为请求 'hit' 结果而处理的任何事情。现在看起来像:

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())

没有必要加上a和b,因为一个是无意义的字符串(花色),一个是有值的int。添加了另一个列表:hit[],通过 hit.append(rand_card()) 添加。现在对于检查以确保我不会两次绘制同一张牌的循环,我添加了:

 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

最后的结果是:

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

我还没有实现游戏的其余部分(半身像,可能是庄家的牌,等等......)但现在一切都按预期进行!