[英]int object is not an iterable python problem
所以我不断得到一个
File "main.py", line 77, in <module>
dealer_cards_total = sum(dealer_cards)
TypeError: 'int' object is not iterable
我尝试了各种“解决方案”并尝试分析其他与我有相同错误的程序,但我似乎找不到任何东西,我根据程序使用一长串标签标记了错误的来源,这里是代码:
import random
import time
Ace = 11
Jack = 10
Queen = 10
King = 10
cards = [Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King]
e = 0
Ace_21 = False
player_bal = 0
dealer_bal = 0
player_cards = []
dealer_cards = []
player_cards_total = 0
dealer_cards_total = 0
card = ''
move = ''
moves = 0
def get_card():
return(int(cards[random.randrange(1, 13)]))
dealer_cards = [get_card()]
player_cards = [get_card(), get_card()]
player_cards_total = sum(player_cards)
def get_move():
if moves == 0:
print('\nDealer\'s cards: ', dealer_cards)
print('Your cards: ', player_cards,'\nTotal: ', player_cards_total)
move = input('Chose your next move: ')
if move in ['h', 'Hit']:
move = 'hit'
elif move in ['s', 'Stand']:
move = 'stand'
return(move)
while player_cards_total < 21:
player_cards_total = sum(player_cards)
dealer_cards_total = sum(dealer_cards)
if player_cards_total > 20:
print('\n\n//////////////////////////\nDealer\'s cards: ', dealer_cards)
print('Your cards: ', player_cards,'\nTotal: ', player_cards_total, '\n//////////////////////////')
print('\nBUST\n')
break
move = get_move()
if move == 'hit':
player_cards.append(get_card())
else:
break
######################################### lines:
if player_cards_total > 21: 66
print('You lose!!!') 67
elif player_cards_total == 21: 68
print('Great job, you win') 69
else: 70
print('DEALER\'S TURN') 71
while dealer_cards_total < 20: 72
if e == 0: 73
dealer_cards.append(get_card()) 74
e = 1 75
print(dealer_cards) 76
dealer_cards_total = sum(dealer_cards) 77
dealer_cards = get_card() 78
#####################################################
if dealer_cards_total == 21:
print('YOU LOSE')
elif dealer_cards_total > 21:
print('DEALER BUSTED\nYOU WIN!!!')
elif dealer_cards_total > player_cards_total and dealer_cards_total < 22:
print('YOU LOSE')
else:
print('YOU WIN')
这可能是一个简单的解决方法,但我是个初学者,我以前没有遇到过这个问题
错误发生在这些行中:
dealer_cards_total = sum(dealer_cards)
dealer_cards = get_card()
这样做的原因是 sum() 需要一个列表。
但是,get_card() 返回一个 integer。因此,在第一次迭代之后,dealer_cards 从列表更改为 integer。
似乎您在脚本开头就考虑到了这一点:
dealer_cards = [get_card()]
在不评估脚本的 rest 的情况下,似乎至少解决方案应该位于某个地方,确保 dealer_cards 仍然是一个列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.