Pai*_*alo -1 python random indexing list python-2.7
我想在python中制作一个二十一点游戏.我遇到了一个问题,因为我试图在我的游戏中使用随机模块.我使用随机模块得到一个与我的列表中的索引号坐标的数字.我制作的清单包括卡面值.不过,我不知道如何使用随机索引号打印这些值.这是我的代码:
# this is a blackjack game made in python
import random
import time
# make a list full of the card values
cards = (["A", "K", "Q", "J", 2, 3, 4, 5, 6, 7, 8, 9, 10])
indexNum1 = random.randint(0,12)
indexNum2 = random.randint(0,12)
indexNum3 = random.randint(0,12)
indexNum4 = random.randint(0,12)
indexNum5 = random.randint(0,12)
for card in cards:
print card(indexNum1)
print card(indexNum2)
print card(indexNum3)
print card(indexNum4)
print card(indexNum5)
Run Code Online (Sandbox Code Playgroud)
我希望有人可以帮我解决这个问题.谢谢!
您可以cards直接索引,例如:
print(cards[indexNum1])
Run Code Online (Sandbox Code Playgroud)
但是如果你想循环,你应该迭代索引:
for cardidx in (indexNum1, indexNum2, indexNum3, indexNum4, indexNum5):
print(cards[cardidx])
Run Code Online (Sandbox Code Playgroud)
但是你要比你需要的更难,因为目前你的代码可以返回5Aces - 我认为你不需要:
cards = ["A", "K", "Q", "J", 2, 3, 4, 5, 6, 7, 8, 9, 10]
hand = random.sample(cards, k=5)
for card in hand:
print(card)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
161 次 |
| 最近记录: |