卡片生成器游戏打印在一列,而不是一排

时间:2015-03-19 04:17:55

标签: python comma sys

#-*- coding: UTF-8 -*-
#spade=u"\u2660"
#heart=u"\u2665"
#diamond=u"\u2666"
#clubs=u"\u2663"
import random
print 'Welcome to SANDCRAB. This is your deck of cards.'
seg1='''_________'''
seg2='''|       |'''
seg3a="|  Ace  |"
seg32="|   2   |"
seg33="|   3   |"
seg34="|   4   |"
seg35="|   5   |"
seg36="|   6   |"
seg37="|   7   |"
seg38="|   8   |"
seg39="|   9   |"
seg310="|  10   |"
seg3jack="|  Jack |"
seg3queen="| Queen |"
seg3king="|  King |"
seg4='''|  of   |'''
seg5s="| Spade |\n|♠♠♠♠♠♠♠|"
seg5h="| Heart |\n|♥♥♥♥♥♥♥|"
seg5c="| Clubs |\n|♣♣♣♣♣♣♣|"
seg5d="|Diamond|\n|♦♦♦♦♦♦♦|"
seg6='''|       |'''
seg7='''|_______|'''
a=[seg3a,seg32,seg33,seg34,seg35,seg36,seg37,seg38,seg39,seg310,seg3jack,seg3queen,seg3king]
b=[seg5s,seg5h,seg5c,seg5d]
count=0
print seg1*13,'\n',
print seg2*13
print "".join([random.choice(a)for i in range(13)])
print seg4*13
pr= "\n".join([random.choice(b)for i in range(13)])
print pr
print seg7*13

您好。我正在尝试制作一个生成13张随机卡的卡片生成器。我没有使用特殊字符就让它工作但现在又出现了新问题。每当我运行程序时,'seg5'的东西都打印在一列中,我不能让它们并排打印。逗号不起作用,我不确定我是否正在使用sys错误,但这也不起作用。感谢。

1 个答案:

答案 0 :(得分:1)

问题是您在seg5*seg5sseg5h,...)中包含换行符吗?

最简单的解决方法是在没有换行元素的情况下制作这些变量列表,然后分别打印每一行。

为此,我首先将seg5*更改为:

seg5s=["| Spade |", "|♠♠♠♠♠♠♠|"]
seg5h=["| Heart |", "|♥♥♥♥♥♥♥|"]
seg5c=["| Clubs |", "|♣♣♣♣♣♣♣|"]
seg5d=["|Diamond|", "|♦♦♦♦♦♦♦|"]

然后生成0< = i< = 3的索引列表,每张卡一个:

suit_sels = [random.randint(0,3) for i in range(13)]

然后,当您想要打印它们时,您可以使用:

# Print name
print "".join([b[i][0] for i in suit_sels])
# Print suit symbols
print "".join([b[i][1] for i in suit_sels])

完整代码:

#-*- coding: UTF-8 -*-
#spade=u"\u2660"
#heart=u"\u2665"
#diamond=u"\u2666"
#clubs=u"\u2663"
import random
print 'Welcome to SANDCRAB. This is your deck of cards.'
seg1='''_________'''
seg2='''|       |'''
seg3a="|  Ace  |"
seg32="|   2   |"
seg33="|   3   |"
seg34="|   4   |"
seg35="|   5   |"
seg36="|   6   |"
seg37="|   7   |"
seg38="|   8   |"
seg39="|   9   |"
seg310="|  10   |"
seg3jack="|  Jack |"
seg3queen="| Queen |"
seg3king="|  King |"
seg4='''|  of   |'''
seg5s=["| Spade |", "|♠♠♠♠♠♠♠|"]
seg5h=["| Heart |", "|♥♥♥♥♥♥♥|"]
seg5c=["| Clubs |", "|♣♣♣♣♣♣♣|"]
seg5d=["|Diamond|", "|♦♦♦♦♦♦♦|"]
seg6='''|       |'''
seg7='''|_______|'''
a=[seg3a,seg32,seg33,seg34,seg35,seg36,seg37,seg38,seg39,seg310,seg3jack,seg3queen,seg3king]
b=[seg5s,seg5h,seg5c,seg5d]
count=0
print seg1*13,'\n',
print seg2*13
print "".join([random.choice(a) for i in range(13)])
print seg4*13
suit_sels = [random.randint(0,3) for i in range(13)]
# Print name
print "".join([b[i][0] for i in suit_sels])
# Print suit symbols
print "".join([b[i][1] for i in suit_sels])
print seg7*13

输出:

Welcome to SANDCRAB. This is your deck of cards.
_____________________________________________________________________________________________________________________ 
|       ||       ||       ||       ||       ||       ||       ||       ||       ||       ||       ||       ||       |
|   5   ||  10   ||  10   ||   9   ||   7   ||   6   ||   6   ||  Ace  ||   4   ||   7   ||   8   ||   2   ||  10   |
|  of   ||  of   ||  of   ||  of   ||  of   ||  of   ||  of   ||  of   ||  of   ||  of   ||  of   ||  of   ||  of   |
| Clubs || Heart || Heart || Clubs || Heart || Spade || Clubs || Spade || Heart || Clubs || Clubs || Heart ||Diamond|
|♣♣♣♣♣♣♣||♥♥♥♥♥♥♥||♥♥♥♥♥♥♥||♣♣♣♣♣♣♣||♥♥♥♥♥♥♥||♠♠♠♠♠♠♠||♣♣♣♣♣♣♣||♠♠♠♠♠♠♠||♥♥♥♥♥♥♥||♣♣♣♣♣♣♣||♣♣♣♣♣♣♣||♥♥♥♥♥♥♥||♦♦♦♦♦♦♦|
|_______||_______||_______||_______||_______||_______||_______||_______||_______||_______||_______||_______||_______|

值得注意的是,你可以简单地简化这段代码,并通过使用列表并生成卡片来减少你使用的变量数量" number"以编程方式行,例如:

#-*- coding: UTF-8 -*-
import random

NUM_CARDS = 13
CARD_WIDTH = 8
SUITS = [('Spades', '♠'), ('Hearts', '♥'), ('Clubs', '♣'), ('Diamonds', '♦')]
NAMES = {11: 'Jack', 12: 'Queen', 13: 'King', 14: 'Ace'}

def get_card_name(n):  return NAMES.get(n, "%2d" % n)
def card_row(text=''): return "|" + text.center(CARD_WIDTH) + "|"

# Generate card output
cards = [list() for _ in range(7)]
for _ in range(NUM_CARDS):
    cv = random.randint(2,14)   # Card Value
    cs = random.randint(0,3)    # Card Suit
    cards[0].append('_' * (CARD_WIDTH+2))
    cards[1].append(card_row(''))
    cards[2].append(card_row(get_card_name(cv)))
    cards[3].append(card_row('of'))
    cards[4].append(card_row(SUITS[cs][0]))
    cards[5].append(card_row(SUITS[cs][1]*(CARD_WIDTH)))
    cards[6].append(card_row("_" * CARD_WIDTH))

# Print card output
print 'Welcome to SANDCRAB. This is your deck of cards.'
for lst in cards:
    print ''.join(lst)