为什么这个 Python 代码在没有任何问题的情况下执行后向我显示“TypeError: 'int' object is not iterable”?

时间:2021-01-27 20:32:00

标签: python list typeerror

我的代码运行得很好,但是在执行完所有内容后,最后总是出现错误。由于我是初学者,我不知道为什么 xD。如果有人可以提供帮助,那就太酷了,我现在只是觉得很愚蠢 xD。 非常感谢提前(: 这也是它显示的完整错误消息:

你的牌:[8, 9],当前分数:17

电脑第一张卡:5

输入'y'获得另一张卡片,输入'n'传递:y

你的牌:[8, 9, 3],当前分数:20

电脑第一张卡:5

输入'y'获得另一张卡片,输入'n'传递:y

你的牌:[8, 9, 3, 9],当前分数:29

电脑第一张卡:5

你的最后一手牌:[8, 9, 3, 9],最终得分:29

电脑最后一手:[5, 3],最终得分:8

你过去了。你输了?

Traceback (most recent call last): 

  File "main.py", line 120, in <module>
    game()
  File "main.py", line 108, in game
    checks_who_wins(total_score_player, added_cards_value(cards_computer)) 
  File "main.py", line 68, in checks_who_wins
    is_blackjack(added_cards_value_player, added_cards_value_computer)
  File "main.py", line 47, in is_blackjack
    if added_cards_value(list_name_player) == 21:
  File "main.py", line 43, in added_cards_value
    list_add = sum(list_name)
TypeError: 'int' object is not iterable




from art import logo 
import random
from replit import clear


def eleven_card(cards_list_porc):
    a = random.randint(0,12)
    if a == 11:
        b = 21 - added_cards_value(cards_list_porc) 
        return b 
    return a

def added_cards_value(list_name):
    # """Calculate and adds every element of the list"""
    # list_add = 0
    # for x in range(0,len(list_name) ):
    #     list_add += list_name[x] 
    list_add = sum(list_name)
    return list_add

def is_blackjack(list_name_player, list_name_computer):
    if added_cards_value(list_name_player) == 21:
        print("Win with a Blackjack ?")
        return "a"
    if added_cards_value(list_name_computer) == 21:
        print("Lose, opponent has Blackjack ?")
        return "a"

def checks_who_wins(added_cards_value_player, added_cards_value_computer):
    if added_cards_value_player < 21 and added_cards_value_computer < 21:
        if added_cards_value_player > added_cards_value_computer:
            print("You win ?") 
        if added_cards_value_player < added_cards_value_computer:
            print("You lose ?")
        if added_cards_value_player == added_cards_value_computer:
            print("Draw ?")
    if added_cards_value_player > 21:
        print("You went over. You lose ?")
    if added_cards_value_computer > 21:
        print("Opponent went over. You win ?")
    if added_cards_value_player == added_cards_value_computer:
            print("Draw ?")
    is_blackjack(added_cards_value_player, added_cards_value_computer)

def end_program(cards_player, cards_computer):
    print(f"Your final hand: {cards_player}, final score: {added_cards_value(cards_player)} ")
    print(f"Computer's final hand: {cards_computer}, final score: {added_cards_value(cards_computer)} ")
    y = False

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
def game():
    play_decision = input("Do you want to play a game of Blackjack? Type 'y' or 'n': ")
    if play_decision == "n":
        return
    if play_decision == "y":
        clear()
        print(logo)

        cards_computer = []
        for x in range (1,3):
            cards_computer.append(cards[random.randint(0,12)])
        cards_player = []
        for x in range (1,3):
            cards_player.append(cards[random.randint(0,12)])
        total_score_player = added_cards_value(cards_player)
        print(f"Your cards: {cards_player}, current score: {total_score_player}")
        print(f"Computer's first card: {cards_computer[0]}")
        if is_blackjack(cards_player, cards_computer) == "a":
            game()
            return
        goes_on = input("Type 'y' to get another card, type 'n' to pass:")
        y = True

        while y == True:
            if goes_on == "y":
                cards_player.append(cards[eleven_card(cards_player)])
                total_score_player = added_cards_value(cards_player)
                print(f"Your cards: {cards_player}, current score: {total_score_player}")
                print(f"Computer's first card: {cards_computer[0]}")
                is_blackjack(cards_player, cards_computer)
                if total_score_player > 21: 
                    end_program(cards_player, cards_computer)
                    checks_who_wins(total_score_player, added_cards_value(cards_computer)) 
            if goes_on == "n":
                while added_cards_value(cards_computer) < 20:
                    cards_computer.append(cards[eleven_card(cards_computer)])
                end_program(cards_player, cards_computer)
                checks_who_wins(total_score_player, sum(cards_computer))  
            if total_score_player < 21 and goes_on == "y":
                goes_on = input("Type 'y' to get another card, type 'n' to pass:")
            else:  
                y = False
        game()

game()

1 个答案:

答案 0 :(得分:0)

调用您的函数 is_blackjack 时出现问题:

is_blackjack(added_cards_value_player, added_cards_value_computer)

这里传递了 added_cards_values(都是整数),而函数需要一个卡片列表。在您的代码中,该函数需要两张卡片列表并尝试计算它们的 added_card_values。因此,更改 is_blackjack() 的函数定义,使其不再尝试计算此总和,因为您已经传递了此总和:

def is_blackjack(added_cards_value_player, added_cards_value_computer):
    if added_cards_value_player == 21:
        print("Win with a Blackjack ?")
        return "a"
    if added_cards_value_computer == 21:
        print("Lose, opponent has Blackjack ?")
        return "a"

你会没事的。