2015-04-05 35 views
1

我需要帮助找到一个python函数,它只会显示元组中的值,(x)次数。python只显示循环中的元组元素x的时间量

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 

感谢。

+0

你没有的功能,虽然/ – khajvah 2015-04-05 17:12:04

+0

对不起,我的意思方法......我试过数(),但它只会从元组中打印出现一次,从元组中循环1个值。 – SicLeaf 2015-04-05 17:21:49

+0

while循环是多余的。它会在没有它的情况下循环适当的时间。 – 2015-04-05 17:35:25

回答

0

添加两行,试试吧:

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 

让我知道,如果它满足您的所有需求。

+0

你有魔力,谢谢。 – SicLeaf 2015-04-05 17:43:25

0

你也可以有所变化你的逻辑和实际发牌,如:

from itertools import product 
from random import * 
rankName = ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", 
      "Nine", "Ten", "Jack", "Queen", "King") 
suit = ("hearts", "diamonds", "spades" , "clubs") 
users = ("deck", "I", "computer") 

NUMCARDS = 52 
DECK = 0 
PLAYER = 1 
COMP = 2 

deck = list(product(suit, rankName)) 
deal = sample(deck, 10) 
player = deal[0::2] 
computer = deal[1::2] 

for count, (suit, rank) in enumerate(deck): 
    user = PLAYER if (suit, rank) in player else COMP if (suit, rank) in computer else DECK 
    print count+1, '\t| ', rank,' of', suit, '\t|', users[user]