【发布时间】:2016-05-25 21:03:01
【问题描述】:
当我在 iPython 中测试此代码时,我收到一个类型错误“int”对象不可迭代。专门指向这几行代码----->
from __future__ import print_function
import random
deck = ['2s', '3s', '4s', '5s', '6s', '7s', '8s', '9s', '10s', 'Js', 'Qs', 'Ks', 'As'] *#Set of cards*
def score_hand(hand): # defines the score of the individual cards and player total*
score=0
for card in hand: # <------
f = card[0] # This seems to be where the problem is but I don't understand it*
if f == 'A':
if score>=20:
score += 1
else:
score +=11
elif f == 'J' or f == 'Q' or f == 'K':
score += 10
elif f == '1':
score += 10
else:
score += int(f)
return score
def print_hand(hand):
print('cards = ', end = ' ')
for card in hand:
print(card, end=' ')
score = score_hand(hand)
print(' score = ', score)
def check_bust(hand):
score = score_hand(hand) # <------
if score <= 21:
return True
if score > 21:
return False
def blackjack():
hand = []
score = score_hand(hand)
choice = 'h'
if check_bust(score) == True: # <------
if choice == 'h':
card_num = random.randint(0, len(deck)-1)
card = deck[card_num]
hand.append(card)
deck.remove(deck[random])
print_hand(hand)
choice = raw_input("Enter h for hit or s for stand.")
return check_bust(score)
if choice == 's':
print('compare with dealer')
if check_bust(score)== False:
print('Bust, score =',score)
return
这是应该的
为从牌组列表中随机选择的牌分配一个值
将该牌值加到玩家手牌中
从牌组列表中移除该牌
根据卡片的组合“分数”,如果分数超过 21,它应该停止。
如果分数没有,它应该返回到 check_bust(score) 并重复,直到玩家超过或输入's'停止。
我试图研究这个网站上的许多类似问题,但发现我无法理解。我想知道哪里出了问题('int' object is not iterable 是什么意思)以及如何修复它。
感谢您的宝贵时间。
【问题讨论】:
-
您在此处将分数传递给 check_bust:
if check_bust(score) == True:您应该通过hand
标签: python int typeerror iterable