【问题标题】:Cards on the Table : Python 'int' object is not iterable桌上的卡片:Python 'int' 对象不可迭代
【发布时间】:2016-05-25 21:03:01
【问题描述】:

当我在 iPython 中测试此代码时,我收到一个类型错误“int”对象不可迭代。专门指向这几行代码----->

from __future__ import print_function
import random

deck = ['2s', '3s', '4s', '5s', '6s', '7s', '8s', '9s', '10s', 'Js', 'Qs', 'Ks', 'As'] *#Set of cards*

def score_hand(hand): # defines the score of the individual cards and player total*
    score=0
    for card in hand:    # <------
        f = card[0]      # This seems to be where the problem is but I don't understand it*
        if f == 'A':     
            if score>=20:
                score += 1
            else:
                score +=11
        elif f == 'J' or f == 'Q' or f == 'K':   
            score += 10
        elif f == '1':   
            score += 10
        else:            
            score += int(f)    
    return score

def print_hand(hand):
    print('cards = ', end = ' ')
    for card in hand:
        print(card, end=' ')

score = score_hand(hand)
print('  score = ', score)

def check_bust(hand):
    score = score_hand(hand) # <------
    if score <= 21:
        return True
    if score > 21:
        return False

def blackjack():
    hand = []
    score = score_hand(hand)
    choice = 'h'
    if check_bust(score) == True: # <------
        if choice == 'h':     
            card_num = random.randint(0, len(deck)-1)
            card = deck[card_num]
            hand.append(card)
            deck.remove(deck[random])
            print_hand(hand)
            choice = raw_input("Enter h for hit or s for stand.")
            return check_bust(score)
        if choice == 's':
            print('compare with dealer')
    if check_bust(score)== False:
            print('Bust, score =',score)
             return

这是应该的

  1. 为从牌组列表中随机选择的牌分配一个值

  2. 将该牌值加到玩家手牌中

  3. 从牌组列表中移除该牌

  4. 根据卡片的组合“分数”,如果分数超过 21,它应该停止。

  5. 如果分数没有,它应该返回到 check_bust(score) 并重复,直到玩家超过或输入's'停止。

我试图研究这个网站上的许多类似问题,但发现我无法理解。我想知道哪里出了问题('int' object is not iterable 是什么意思)以及如何修复它。

感谢您的宝贵时间。

【问题讨论】:

  • 您在此处将分数传递给 check_bust:if check_bust(score) == True: 您应该通过 hand

标签: python int typeerror iterable


【解决方案1】:

因此,在您的二十一点函数中,您有一个名为 hand 的空列表,该列表从未填充过......更不用说当您在二十一点中调用 score_hand 时,hand 没有任何内容,因此您试图在没有索引时调用索引 0 中的项目导致该错误的项目...

【讨论】:

  • 你在程序中间插入了这两行奇怪的代码, score = score_hand(hand);打印('分数=',分数)。您应该首先定义所有函数,然后将主程序放在底部。而且你永远不会实例化一手牌,因为你永远不会调用 blackjack 函数。你的 check_bust 向后看, bust 应该 >21 返回 True,所以你有它,二十一点函数永远不会发牌。
  • 我想你也想在二十一点函数中使用一个while循环,while check_bust == False 和choice == 'h':发另一张牌,所以它一直持续到玩家进入stay 或他破产了。
【解决方案2】:

鉴于此程序中语义和逻辑错误的数量,我建议您采用增量编程的做法:编写几行代码,测试它们,纠正错误,并确保在继续之前有很多工作。就目前而言,您现在有 50 行代码,其中一些错误掩盖了其他错误,一些错误相互依赖,您现在对出了什么问题感到困惑。

另外,学习basic debugging。我最担心的是你在这个过程中可能损失的时间。最重要的是,在错误回溯中提到的每个语句前面都应该有一个有意义的 print 语句,显示故障时适用变量的值。这种简单的技术可以让您找到最初的几个问题,而不是浪费时间发布这个问题并等待我们回复。

我已经对您的代码进行了初步修复:

  • 修复了 print_hand 中的缩进
  • 修复了各种 PEP-8 差异(编码风格)
  • 将丢失的调用添加到您的输入例程(最后一行)
  • 在对 check_bust 的两次调用中将 score 替换为 hand
  • 删除了布尔检查的冗余部分(例如 flag == True
  • 修正了您尝试选择随机卡片的问题(当前为第 51 行)。
  • 删除了 blackjack 末尾无用的 return

计划远未完成。例如,blackjack 的一个分支返回一个布尔值;其他人自然而然地返回。该 return 语句在处理第二张牌后结束游戏。

我把这些留给你;我希望你继续编程。你已经做到了这一点正确

from __future__ import print_function
import random

deck = ['2s', '3s', '4s', '5s', '6s', '7s', '8s', '9s', '10s', 'Js', 'Qs', 'Ks', 'As']  # Set of cards*


def score_hand(hand):   # defines the score of the individual cards and player total*
    score = 0
    for card in hand:
        f = card[0]     # This seems to be where the problem is but I don't understand it*
        if f == 'A':
            if score >= 20:
                score += 1
            else:
                score += 11
        elif f == 'J' or f == 'Q' or f == 'K':
            score += 10
        elif f == '1':
            score += 10
        else:
            score += int(f)
    return score


def print_hand(hand):
    print('cards = ', end=' ')
    for card in hand:
        print(card, end=' ')

    score = score_hand(hand)
    print('  score = ', score)


def check_bust(hand):
    score = score_hand(hand)
    if score <= 21:
        return True
    if score > 21:
        return False


def blackjack():
    hand = []
    score = score_hand(hand)
    choice = 'h'
    if check_bust(hand):
        if choice == 'h':
            card_num = random.randint(0, len(deck)-1)
            card = deck[card_num]
            hand.append(card)
            deck.remove(deck[random.randint(0, len(deck)-1)])
            print_hand(hand)
            choice = raw_input("Enter h for hit or s for stand.")
            return check_bust(hand)
        if choice == 's':
            print('compare with dealer')
    if not check_bust(score):
        print('Bust, score =', score)
        return

blackjack()

【讨论】:

    猜你喜欢
    • 2015-04-14
    • 2022-07-01
    • 2015-04-06
    • 2013-10-31
    • 2014-11-04
    • 2016-02-24
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多