纸牌生成器游戏打印在一列,而不是一行

Card generator game printing in a column, not a row

#-*- 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 但这也不起作用。谢谢

问题是您在 seg5* (seg5s, seg5h, ...)

中包含换行符

最简单的解决方法是制作这些变量列表,不带换行元素,然后分别打印每一行。

为此,我首先将 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)