2016-11-02 100 views
0

因此,在我的CS類的介紹中,我們正在學習python,我對此有一點了解,但是我們有一個項目,我們正在向4人交付一副牌,每種牌13張牌,基本上他們打橋牌,明智的點值。 當教授想要它說 使用一個名爲顯示的功能打印列表中的數字 長問題,主要是代碼,謝謝你的幫助,而且,如果反正清理它我喜歡建議!Verticle Print需要幫助刪除空間

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("_______________") 
+0

裏面'reveal'打印值,因此您不必返回「」,你可以用' reavel(...)'而不是'print(reveal(...))'。爲了使注意能夠添加一些空格'print(「」,i)「。 – furas

回答

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) 
+0

謝謝!這工作,任何想法如何讓它縮進像他想要的? –

0

print增加了新的行字符全自動每它打印的字符串。要使用別的東西,例如,在一個相同的行打印,只需指定不同的字符,例如:

print(i, end=' ')