所以我不斷得到一個
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')
這可能是一個簡單的解決方法,但我是一個初學者,我以前沒有遇到過這個問題
uj5u.com熱心網友回復:
錯誤發生在這些行中:
dealer_cards_total = sum(dealer_cards)
dealer_cards = get_card()
原因是 sum() 需要一個串列。
但是,get_card() 回傳一個整數。因此,在第一次迭代之后,dealer_cards 從串列變為整數。
您似乎在腳本開頭就考慮了這一點:
dealer_cards = [get_card()]
在不評估腳本的其余部分的情況下,似乎至少解決方案應該存在于確保dealer_cards 仍然是一個串列的地方。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/427807.html
標籤:Python python-3.x 列表 整数
