python只在循环x次中显示元组项 [英] python only show tuple items in loop x amount of times
本文介绍了python只在循环x次中显示元组项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助找到一个 python 函数,它只会显示元组中的值,(x) 次.
I need help finding a python function that will only show the value in the tuple, (x) amount of times.
from random import *
rankName = ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King")
suit = ("hearts", "diamonds", "spades" , "clubs")
users = ("I", "computer", "deck")
NUMCARDS = 52
DECK = 0
PLAYER = 1
COMP = 2
count = 0
while (count < 52):
for u in rankName:
for i in suit:
count = count + 1
w = choice(users)
''' 'computer' and 'I' should only show 5 times, while deck shows 42 cards '''
print count, '\t| ', u,' of', i, '\t|', w
谢谢.
推荐答案
加两行,试试:
from random import *
rankName = ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King")
suit = ("hearts", "diamonds", "spades" , "clubs")
users = ("I", "computer", "deck")
# make it weighted and shuffed
users_with_weights = ["I"]*5 + ['computer']*5 + ['deck']*42
shuffle(users_with_weights)
NUMCARDS = 52
DECK = 0
PLAYER = 1
COMP = 2
count = 0
while (count < 52):
for u in rankName:
for i in suit:
count = count + 1
w = users_with_weights.pop()
''' 'computer' and 'I' should only show 5 times, while deck shows 42 cards '''
print count, '\t| ', u,' of', i, '\t|', w
如果它满足您的所有需求,请告诉我.
Let me know if it meets all your needs.
这篇关于python只在循环x次中显示元组项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文