【发布时间】:2018-08-03 18:19:46
【问题描述】:
deck = ['1c', '4s', '8s', '8h', '1h', '2s', '2c', '8h', 'ks', 'qd', '4d', 'jd', '7c', '10h', '5c', '10d', '3d', '9c', '7d', '4h', '2s']
powerCard = "1c"
def deckStrength(powerCard, deck):
# global deck
powerCardExists = False
for card in deck:
if card == powerCard:
powerCardExists = True
if(powerCardExists):
deck.remove(powerCard)
for card in deck:
card = card[:-1]
print(deck)
deckStrength(powerCard, deck)
如果你运行它,输出将是:
['4s', '8s', '8h', '1h', '2s', '2c', '8h', 'ks', 'qd', '4d', 'jd', '7c', '10h', '5c', '10d', '3d', '9c', '7d', '4h', '2s']
如您所见,在我的 deckStrength 函数中的最后一个 for 循环中,我试图摆脱我的牌组列表中每个字符串中的最后一个字符。这没有发生,有什么线索吗?
我还想补充一点,我尝试在没有甲板作为函数参数并调用“全局甲板”的情况下执行此操作,但没有奏效,所以我尝试了这个。
【问题讨论】:
-
您正在循环中创建一个新字符串并将其丢弃。在任何情况下,字符串在 Python 中都是不可变的。
标签: python python-3.x for-loop