So I keep getting a
File "main.py", line 77, in <module>
dealer_cards_total = sum(dealer_cards)
TypeError: 'int' object is not iterable
I've tried various 'solutions' and tried analyzing other programs that have the same error as mine but I can't seem to find anything,I marked where the error is coming from according to the program using a long line of hashtags, here's the code:
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')
It's probably an easy fix but I'm kind of a begginer and I haven't gotten this problem before
CodePudding user response:
The error occurs in these lines:
dealer_cards_total = sum(dealer_cards)
dealer_cards = get_card()
The reason for this is that sum() expects a list.
However, get_card() returns an integer. As such, after the first iteration, dealer_cards changes from a list to an integer.
It seems like you accounted for this at the beginning of the script:
dealer_cards = [get_card()]
Without evaluating the rest of your script, it seems at least the solution should lie somewhere with making sure that dealer_cards remains a list.