Python “int”对象不可迭代

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28017142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:37:24  来源:igfitidea点击:

'int' object not iterable

pythonintiterable

提问by Dhruv S

i've been trying to get the sum of list that changed its values from a string to int using a function

我一直在尝试使用函数获取将其值从字符串更改为 int 的列表的总和

player_hand = [] 

def card_type(player_hand):
 card_value = 0
 if player_hand[0] == 'A':
     card_value = 11
 if player_hand[0] == 'J':
     card_value = 10
 if player_hand[0] == 'Q':
     card_value = 10
 if player_hand[0] == 'K':
     card_value = 10

 if player_hand[0] == '2':
     card_value = 2
 if player_hand[0] == '3':
     card_value = 3
 if player_hand[0] == '4':
     card_value = 4
 if player_hand[0] == '5':
     card_value = 5
 if player_hand[0] == '6':
     card_value = 6
 if player_hand[0] == '7':
     card_value = 7
 if player_hand[0] == '8':
     card_value = 8
 if player_hand[0] == '9':
     card_value = 9
 if player_hand[0] == '1':
     card_value = 10

def player_hit(card_deck):
 rando = random.randint(0,len(card_deck)-1)
 player_hand.append(card_deck[rando])
 card_deck.remove(card_deck[rando])

and then try to find the sum of the player list using

然后尝试使用

card_total = 0

print('Player was handed:')
for i in range(2):
    print(player_hit(card_deck))

for i in len(player_hand)-1:
    print('\n',sum(card_type(player_hand[i])))

however i get an error

但是我收到一个错误

for i in len(player_hand)-1:
TypeError: 'int' object is not iterable

i don't understand what the problem is because ive taken the values and converted them into int's already as well as checked the list index range. Please help

我不明白问题是什么,因为我已经获取了这些值并将它们转换为 int 并检查了列表索引范围。请帮忙

回答by VHarisop

len(player_hand) - 1is just an integer, but the code you've written tries to loop over it. You need an iterable object to perform a forloop. Try this:

len(player_hand) - 1只是一个整数,但您编写的代码试图遍历它。您需要一个可迭代对象来执行for循环。尝试这个:

 for i in range(len(player_hand)):
     # do your thing here

An alternative would be iterating directly over player_handsince it is iterable, just like this:

另一种方法是直接player_hand迭代,因为它是可迭代的,就像这样:

 for card in player_hand:
     print('\n', card_type(card))

回答by Hugh Bothwell

Here is an object-oriented version which may be easier to work with:

这是一个面向对象的版本,它可能更容易使用:

from random import shuffle

card_value = {
    "A": 11,    "J": 10,    "Q": 10,    "K": 10,    "1": 10,
    "9": 9,     "8": 8,     "7": 7,     "6": 6,     "5": 5,
    "4": 4,     "3": 3,     "2": 2
}

class Deck:
    def __init__(self):
        self.d = list(card_value) * 4
        shuffle(self.d)

    def hit(self):
        return self.d.pop()

class Player:
    def __init__(self, name):
        self.name = name
        self.hand = []

    def hit(self, deck, num=1):
        for i in range(num):
            self.hand.append(deck.hit())

    def hand_value(self):
        return sum(card_value[card] for card in self.hand)

    def __str__(self):
        return "{}: {} ({} points)".format(self.name, "".join(self.hand), self.hand_value())

def main():
    print("Fresh deck!")
    deck = Deck()
    # deal 3 cards to player 1 and show the result
    p1 = Player("Charles")
    p1.hit(deck, 3)
    print(p1)        # Charles: Q96 (25 points)

if __name__ == "__main__":
    main()