为了账号安全,请及时绑定邮箱和手机立即绑定

站着选择什么都不做

站着选择什么都不做

宝慕林4294392 2021-11-23 20:08:53
所以我试图在 python 3 中制作一个简单的二十一点游戏,一切都很好,除了我受不了。如果我输入 2 它不会做任何事情。提前致谢。编辑 1:击球效果很好。编辑 2:发布整个脚本,以便您可以重现我面临的问题,正如@roganjosh 建议的那样。from random import shuffleimport sysdef deal(deck, player, dealer):    shuffle(deck)    for _ in range(2):        player.append(deck.pop())        dealer.append(deck.pop())def score(hand):    non_aces = [c for c in hand if c != 'A']    aces = [c for c in hand if c == 'A']    sum = 0    for card in non_aces:        if card in 'JQK':            sum += 10        else:            sum += int(card)    for card in aces:        if sum <= 10:            sum += 11        else:            sum += 1    return sumdef display_info(player, dealer, stand):    print("Your cards: [{}] ({})".format(']['.join(player), score(player)))    if stand:        print("Dealer cards: [{}] ({})".format(']['.join(dealer), score(dealer)))    else:        print("Dealer cards: [{}] [?]".format(dealer[0]))def results(player, dealer, hand, stand):    if score(player) == 21 and hand:        print("Blackjack! You won!")            sys.exit()        elif score(player) > 21:        print("Busted. You lost!")           sys.exit()         if stand:        if score(dealer) > 21:            print("Dealer busted. You won!")        elif score(player) > score(dealer):            print("You beat the dealer! You won!")        elif score(player) < score(dealer):            print("You lost!")        else:            print("Push. Nobody wins or losses.")        sys.exit()def hit_stand(deck, player, dealer, hand, stand):    print("What would you like to do")    print("[1] - Hit\n[2] - Stand")    choice = input("> ")    hand = False    if choice == '1':        player.append(deck.pop())    elif choice == '2':        stand = True        while score(dealer) <= 16:            dealer.append(deck.pop())
查看完整描述

2 回答

?
不负相思意

TA贡献1777条经验 获得超10个赞

您不是在玩家选择站立后检查结果。由于您只deal()在while True循环之前一次,如果您选择重复站立,您只会得到无限条件。庄家抽完所有牌后计算分数。


def hit_stand(deck, player, dealer, hand, stand):

    print("What would you like to do")

    print("[1] - Hit\n[2] - Stand")

    choice = input("> ")

    hand = False

    if choice == '1':

        player.append(deck.pop())

    elif choice == '2':

        stand = True

        while score(dealer) <= 16:

            print(score(dealer))

            dealer.append(deck.pop())

        display_info(player, dealer, stand)

        results(player, dealer, first_hand, stand) # HERE

顺便说一句,在确定最终得分后退出游戏并不是很优雅。你会想看看比一个更好的结构while True:,并sys.exit()以控制流,但是这是你的一个锻炼。


最后,您不应该sum在内部用作变量名,score()因为这是您正在重新定义的内置函数。使用类似的东西,total这样你就不会冒着掩盖内置函数本身的风险。


查看完整回答
反对 回复 2021-11-23
?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

我想添加另一个注释,因为我认为这是关于 Python 如何工作的一个非常重要的点。这是具有 C/C++/Java 背景的人(即我们几乎所有人)在使用 Python 时需要取消学习的东西。

正如我在上面的评论中所述,在 Maria Laura 的原始代码中,似乎调用hit_stand是打算使用某些变量(例如stand)作为输出变量,而在 Python 中,我们不能在函数调用中使用“输出变量” . 但是 Maria Laura 提到“命中效果很好”,这意味着参数player正在被修改。那么,如果我们不能有“输出变量”,那么为什么player函数修改了值hit_stand

当代码调用 时hit_stand,会向函数传递五个对象:

  • 一个名为 的列表对象deck

  • 一个名为 的列表对象player

  • 一个名为 的列表对象dealer

  • 一个布尔对象,命名为hand

  • 一个布尔对象,命名为 stand

此函数外的代码也有(deck, player, dealer, first_hand, standing)指向这五个相同对象的名称。内的代码hit_stand.append()方法,呼吁playerdealer列表对象和.pop()方法被称为上deck对象,所以所有的对象都发生突变。来自调用范围的名称仍然指向那些相同的对象,因此这些名称现在将看到这些更改。

这个故事的handstand是不同的。在hit_stand函数内部,handstand使用=运算符分配新值。正如Fredrik Lundh 的这篇优秀文章所述=,Python 中的运算符不会“更改”变量,它只需要一个对象并将其绑定到一个名称。 对象本身没有改变,而是被新的布尔对象替换了。所以standing外部作用域中的变量仍然指向它原来的布尔对象,而stand函数内部的变量指向的是一个全新的布尔对象,与外部作用域中的不同。没有什么我们可以做的变量handstand 这将在外部范围内看到,不能像我们在其他语言中那样存在“通过引用传递”或“输出参数”之类的东西。

这是一个起初看起来很陌生的概念,直到我们忘记在 C/C++/Java 教育中学到的东西。


查看完整回答
反对 回复 2021-11-23
  • 2 回答
  • 0 关注
  • 196 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信