gpt4 book ai didi

python - 是否可以在Python中的Range函数运行时更改它的值?

转载 作者:行者123 更新时间:2023-12-01 00:42:37 25 4
gpt4 key购买 nike

Nerd 项目警报!

我正在尝试制作 DND 中的 Deck of Many things 的 Python 编码版本。它工作得很好,只是我希望能够允许玩家在完成抽牌后从同一副牌中抽出更多牌。

我当前的模型不允许他们这样做,并且每次都会将牌组重置回原始设置。

我想保留相同的牌组,并且如果抽到某些牌( clown /傻瓜)等,则允许玩家抽更多牌。

我已经能够将像 clown 这样的卡片放入洗牌中,但我似乎无法让程序停止并允许我继续。我尝试过 Return 函数,但总是出现错误,比如不在循环中。

# playerInput here is whatever number the players put in
#card13 contains all the cards from a deck of many things

for draw in range(playerInput):
cardDrawn = random.choice(card13)
print(cardDrawn)
print()
card13.remove(cardDrawn)

if cardDrawn == (void):
print("Your soul is sucked from your body. Draw no more cards")
break

if cardDrawn == (donjon):
print("You are imprisoned in a location of the DM's choosing. Draw no more cards")
break

elif cardDrawn == (skull):
print("Refer to the DMG to see the Specter of Death's stats")
print()

elif cardDrawn == (fool):
print("the card magically shuffles back into the deck.")
print()
card13.append(cardDrawn)

elif cardDrawn == (jester):
print("the card magically shuffles back into the deck.")
print()
card13.append(cardDrawn)

#for fool and jester I don't know how to get the card to keep drawing.

我希望在代码末尾得到一个提示,如果抽到了 clown 或傻瓜,那么只要牌组中还剩下牌,玩家就可以选择抽更多牌。

感谢您的帮助,这是我的第一个程序,因此我对草率的工作表示歉意。

最佳答案

这就是 while 循环有用的地方,而不是使用带有范围的 for 循环,而是使用带有递减计数器的 while 循环,然后可以在循环期间修改当前值,例如

while playerInput:
# Decrement playerInput value
playerInput -= 1
cardDrawn = random.choice(card13)

if cardDrawn == void:
print("Your soul is sucked from your body. Draw no more cards")
# Cannot draw more cards so decrement all to exit the loop
playerInput = 0

... # removed for brevity

elif cardDrawn == jester:
print("the card magically shuffles back into the deck.")
# Increment the playerInput value as if the card wasn't drawn.
playerInput += 1

while 循环将继续循环,直到条件为 True,在 Python 中,如果整数非零且允许其工作,则将其视为 True

关于python - 是否可以在Python中的Range函数运行时更改它的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57247529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com