【问题标题】:Using functions and random to output two new items from a list. (Python 3.6)使用函数和随机从列表中输出两个新项目。 (Python 3.6)
【发布时间】:2018-09-29 00:16:18
【问题描述】:

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

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 添加到一个字符串中的函数。然而它是一个函数,当它返回时

print ("You are dealt: ", h1)

它出来了

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

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

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

我相信你们中的一些伟大的头脑已经解决了我的具体问题,所以任何帮助都非常感谢!

谢谢。

【问题讨论】:

  • 我想在你的赌场玩——你每副牌有 8 个 A。
  • 大声笑,我稍后会改变它,只是分配一个随机值以避免在我工作时发生冲突。

标签: python function random lambda blackjack


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我在朋友的帮助下想通了(这远远超出了这所学校所能提供的任何东西)!

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

    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
    

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

    【讨论】:

    • 你允许同一张牌在平局中多次使用,对吧?同时像两个黑桃 A 一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    相关资源
    最近更新 更多