Verticle打印需要帮助删除空格

时间:2016-11-02 01:00:13

标签: python

所以在CS课程的介绍中,我们正在学习python,我对此有所了解,但是我们有一个项目,我们正在为4人处理一副卡片,每张卡片13张,基本上他们扮演桥梁,点值明智。 打印教授时要说它 使用一个名为reveal的函数打印出来并在列表中输入数字 很长的问题,主要是代码,谢谢你的帮助,如果你还是要清理它我喜欢的建议!

West
    Diamonds
    J
    A
    Spades
    4
    7
    8
    10
    Q
    Hearts
    4
    9
    Q
    Clubs
    5
    A 
West has a point value of 13 points!
_______________
North
    Diamonds
    9
    10
    Q
    K
    Spades
    2
    3
    6
    J   
    Hearts

就像那样,但我的印刷方式如下:

West
Diamonds
J
A

Spades
4
7
8
10
Q

Hearts
4
9
Q

Clubs
5
A

West has a point value of 13 points!
_______________
North
Diamonds
9
10
Q
K

Spades
2
3
6
J

Hearts

这是完整的代码

import random
Deck_cards = list(range(1,53))
random.shuffle(Deck_cards)

West=Deck_cards[0:13]
West.sort()
North=Deck_cards[13:26]
North.sort()
East=Deck_cards[26:39]
East.sort()
South=Deck_cards[39:52]
South.sort()
players_hand=[West, North, East, South]
Players_handname=["West","North","East","South"]

def facecard(sel):
    if sel % 13==10:
        return "J"
    elif sel % 13==11:
        return "Q"
    elif sel % 13==12:
        return "K"
    elif sel % 13==0:
        return "A"
    else:
        return (sel+1) % 13

def cardpoints(sel):
    value=0
    if sel % 13==10:
        return value+1
    elif sel % 13==11:
        return value+2
    elif sel % 13==12:
        return value+3
    elif sel % 13==0:
        return value+4
    else:
        return value

def totalpoints(x):
    sum=0
    for i in x:
        if i in range(1,53):
            sum=cardpoints(i)+sum
    return sum

def spades(x):
    print("Spades")
    list1 = []
    for i in x:
        if i in range(1,14):
            list1.append(facecard(i))
    return list1

def hearts(x):
    print("Hearts")
    list1 = []
    for i in x:
        if i in range(14,27):
            list1.append(facecard(i))
    return list1

def diamonds(x):
    print("Diamonds")
    list1 = []
    for i in x:
        if i in range(27,40):
            list1.append(facecard(i))
    return list1

def clubs(x):
    print("Clubs")
    list1 = []
    for i in x:
        if i in range(40,53):
            list1.append(facecard(i))
    return list1

def reveal(x):
    for i in x:
        print(i)
    return " "
index = 0
for i in players_hand:
    print(Players_handname[index])
    print(reveal(diamonds(i)))
    print(reveal(spades(i)))
    print(reveal(hearts(i)))
    print(reveal(clubs(i)))
    print(Players_handname[index],"has a point value of", totalpoints(i) , "points!")
    index=index+1
    print("_______________")

2 个答案:

答案 0 :(得分:1)

reveal功能中,您有print个电话,例如print(i)。这个函数返回一个空格。当您拨打reveal时,您正在打印通话结果。如果您不想要它,没有理由返回并打印带空格的额外行。只需不要将reveal电话打包在print

def reveal(x):
    for i in x:
        print(i)

...

reveal(diamonds(i))

如果您想缩进该内容以及删除额外的行,只需在必要时添加标签:

def hearts(x):
    print("\tHearts")
    ...

def reveal(x):
    for i in x:
        print('\t%s' % i)

答案 1 :(得分:0)

print自动为其打印的每个字符串添加新行字符。要使用其他内容,例如,在同一行中打印,只需指定不同的字符,例如:

print(i, end=' ')