Comment utiliser pour Loop pour imprimer des cartes?
-
29-10-2019 - |
Question
J'ai les 52 cartes configurées et j'essaie d'imprimer les 52 cartes en utilisant for loop
. Je ne sais pas comment régler mon for loop
À ce point.
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]
Je veux imprimer comme ça
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
La solution
Votre fonction define_cards
doit retourner la liste. Ajouter return cards
à sa fin.
Ensuite, vous devez réellement appeler / exécuter cette fonction.
Ensuite, vous pouvez accéder aux cartes individuelles dans cette liste:
cards = define_cards()
for i, card in enumerate(cards):
print i, card
Cependant, si vous recherchez une solution "plus pythonique", essayez ceci:
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)
Autres conseils
Regardez juste ça
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]
Pourquoi imprimer card_string[i]
?
Quel est le problème avec cards[i]
?
ranks = ("ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king")
suits = ("clubs","diamonds","hearts","spades")
La réponse est une élégante liner:
cards = [rank+' of '+suit for suit in suits for rank in ranks]
for i,card in enumerate(cards):
print i, card
Résultat:
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()
Pourquoi ne pas utiliser un itérateur:
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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow