Domanda

Ho tutte e 52 le carte configurate e cerco di stampare tutte e 52 le carte utilizzando for loop. Non so come impostare il mio for loop a questo punto.

def define_cards(n):
    rank_string = ("ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king")
    suit_string = ("clubs","diamonds","hearts","spades")
    cards = []
    for suit in range(4):
        for rank in range(13):
            card_string = rank_string[rank] + " of " + suit_string[suit]
            cards.append(card_string)

print "The cards are:"
for i in range(52):              #how to make this for loop work??
    print i, card_string[i]

Voglio stampare in questo modo

The crads are:
0 ace of clubs
1 two of clubs
2 three of clubs
...
49 jack of spades
50 queen of spades
51 king of spades
È stato utile?

Soluzione

La tua funzione define_cards deve restituire l'elenco.Aggiungi return cards alla sua fine.

Quindi devi effettivamente chiamare / eseguire questa funzione.

Quindi puoi accedere a singole carte in questo elenco:

cards = define_cards()
for i, card in enumerate(cards):
    print i, card
.

Tuttavia, se stai cercando una soluzione "più pitone", prova questo:

import itertools as it

rank_string = ("ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king")
suit_string = ("clubs","diamonds","hearts","spades")

print 'The cards are:'
for i, card in enumerate(it.product(rank_string, suit_string)):
    print i, '{0[1]} of {0[0]}'.format(card)
.

Altri suggerimenti

Guarda solo questo

    cards.append(card_string)

print "The cards are:"
for i in range(52):              #how to make this for loop work??
    print i, card_string[i]
.

Perché stampare card_string[i]?

Cosa c'è di sbagliato con cards[i]?

ranks = ("ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king")
suits = ("clubs","diamonds","hearts","spades")

La risposta è un'elegante battuta:

cards = [rank+' of '+suit for suit in suits for rank in ranks]

for i,card in enumerate(cards):
    print i, card

Risultato:

0 ace of clubs
1 two of clubs
...
50 queen of spades
51 king of spades
def define_cards():
    rank_string = ("ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king")
    suit_string = ("clubs","diamonds","hearts","spades")
    cards = []
    n = 0
    for suit in suit_string:
        for rank in rank_string:
            print '%s %s of %s' % (n,rank,suit)
            n+=1

define_cards()
.

Perché non utilizzare un iteratore:

def define_cards():
    rank_string = ("ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king")
    suit_string = ("clubs","diamonds","hearts","spades")
    for suit in suit_string:      # you can obtain the items of the iterate list directly, no need of rank access
        for rank in rank_string:
            card_string = rank + " of " + suit
            yield card_string

print "The cards are:"
cards_iterator = define_cards()
for i, card in enumerate(cards_iterator):   # use the iterator power ;)
    print i, card
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top