@kakaricho_hosa

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Python:エラーを直す方法を知りたい(TypeError: 'function' object is not iterable)

Python:エラーを直す方法を知りたい

超初心者です。とある本を参照しながら、でコードを書いています。
にもかかわらず以下のエラーが発生してしまい、直し方がわからず困っております。
成果物はコマンドプロンプト上でブラックジャックゲームができるというものです。

発生している問題・エラー

Traceback (most recent call last):
  File XXXXX, line 80, in <module>
    main()
  File XXXXX, line 64, in main
    print_player_hand(player_hand)
  File XXXXX, line 22, in print_player_hand
    for card in print_player_hand:
TypeError: 'function' object is not iterable

該当するソースコード

import random

RANK , SUIT = 0 , 1
def get_point(hand):
    result = 0
    ace_flag = False
    for card in hand:
        if card[RANK] == 1:
            ace_flag = True
        if card[RANK] > 10:
            num = 10
        else:
            num =card[RANK]
        result = result + num
    if ace_flag and result <= 11:
        result += 10
    return result

#print_player_hand関数
def print_player_hand(player_hand):
    print("プレイヤー(",get_point(player_hand),"):")
    for card in print_player_hand:
        print("[",card[SUIT],card[RANK],"]")
    print()

#print_dealer_hand関数
def print_dealer_hand(dealer_hand,uncovered):
    if uncovered:
        print("ディーラー(",get_point(dealer_hand),"):")
    else:
        print("ディーラー(??):")
    flag = True
    for card in dealer_hand:
        if flag or uncovered:
            print("[",caard[SUIT],card[RANK],"]")
            flag = False
        else:
            print("{**}")
    print()

def make_deck():
    suits = ["S","H","D","C"] #記号
    ranks = range(1,14) #数字
    deck = [(x,y) for x in ranks for y in suits]
    random.shuffle(deck)
    return(deck)

def main():
    turn = 1
    player_money = 100
    while(player_money > 0):
        print("ターン:",turn)
        print("所持金:",player_money)

        player_hand = [] #プレイヤーの手札を格納するリスト
        dealer_hand = [] #ディーラーの手札を格納するリスト
        deck = make_deck()
        # print(deck)
        for i in range(2):
            player_hand.append(deck.pop())
            dealer_hand.append(deck.pop())

        #print(player_hand)
        print_player_hand(player_hand)

        #print_(dealer_hand)
        print_dealer_hand(dealer_hand,False)

        print(player_hand)
        print(get_point(player_hand))
        print(dealer_hand)
        print(get_point(dealer_hand))


        turn += 1
        input("次のターンへ")
    print("ゲームオーバー")

if __name__ == "__main__":
    main()

自分で試したこと

手元のテキストを確認して該当の行を再度書き直したのですが、改善しませんでした。

皆様へ

単純なことだと思うのですが、素人の私には解決方法がわらからず困っております。
関数や変数が多く結局どこまで立ち返るのかも、まだピンときていないです。
解決方法と、このようなエラーが発生したときの確認の手順などご教示いただけると幸いです。
どうぞよろしくお願いいたします。

0 likes

2Answer

Comments

  1. @kakaricho_hosa

    Questioner

    @_y_s様
    早速のご確認、ありがとうございます!
    ご指摘の箇所を修正したら直りました!そして、私の確認ミスでした・・・
    お恥ずかしい限りです。ご教示いただきましてありがとうございます。

    教えていただいた内容をもとにテキストの解説を再確認したところ、
    inの後ろは「複数の値を持つデータ構造のオブジェクト」と記載されておりました。

    単なる確認ミスで申し訳なかったのですが、
    教えていただけたおかげで理解を深めることができました。

    これからいっそう精進します。
    ド素人で恐縮ですが引き続きよろしくお願いいたしますm(_ _)m

エラーが出た場合、エラーの最後の行を見ると良いですよん!

今回の場合はここです。

File XXXXX, line 22, in print_player_hand
    for card in print_player_hand:

File XXXXX の22行目で処理が完了できなかったよ〜って教えてくれます。

エラーの内容については、その次にあるTypeErrorを見ると良いです。

TypeError: 'function' object is not iterable

このTypeErrorは、いろんな種類がありますが、だいたい同じ様なのがでるので、そのうちタイプ毎にどこが悪いんだなぁ〜ってのが分かってくるようになるはずです(^^)

最初のうちはエラーが怖いかもしれませんが、いつの間にかエラーに感謝するようになると思います。(エラーの仕組み考えた人すげぇ〜・・・感謝!)

0Like

Comments

  1. @kakaricho_hosa

    Questioner

    @NOIZEさん
    コメントありがとうございます。
    また、返信おそくなり失礼いたしました。

    なるほど、最後の行を見るのですね・・・!
    投稿前もエラーは出ていて、なんとなくここが悪いのかなーと
    想像で対処してきていたので、最後の行をよく確認するようにします!

    たしかに、エラーの仕組み考えた人すごいです。
    失敗から勉強しようと思います!
    また困ったときは助けていただけると嬉しいです。ありがとうございました。

Your answer might help someone💌