2016-10-20 41 views
-1
import random 

cards_names = {1: "Ace", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 
       9: "9", 10: "10", 11: "Jack", 12: "Queen", 13: "King"} 

def dealing(): 
    return random.randint(1, 13) 

def value_of_hand(cards): 
    value = 0 
    for card in cards: 
     if 1 < card <= 10: 
      value += card 
     elif card > 10: 
      value += 10 

    if 1 in cards and value + 11 <= 21: 
      return value + 11 
    elif 1 in cards: 
      return value + 1 
    else: 
      return value 

def your_hand(name , cards): 
    faces = [cards_names[card] for card in cards] 
    value = value_of_hand(cards) 

    if value == 21: 
     print ("Wow, you got Blackjack!") 
    else: 
     print ("") 

    print ("%s's hand: %s, %s : %s %s") % (name, faces[0], faces[1], value) 

for name in ("Dealer", "Player"): 
    cards = (dealing(), dealing()) 
    your_hand(name, cards) 
+2

我給你一個你很可能想得到的答案,但爲了將來的參考,我建議你閱讀[如何問](http://stackoverflow.com/help/how-to-ask)。簡而言之:準確地說明您遇到的錯誤,您遇到問題的部分代碼以及代碼所需的行爲。歡迎來到Stackoverflow! –

回答

1

我在此假設您正在使用Python 3.x和收到此錯誤:

TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'

你必須%,此前移動的部分版畫括號內,以避免錯誤。另外,您的印刷品中有太多的%s。卸下,然後將其打印罰款:

print ("%s's hand: %s, %s : %s" % (name, faces[0], faces[1], value)) 

Dealer's hand: Ace, 8 : 19

Player's hand: 9, 7 : 16

正如你所看到的,%s數量:■應該等於你提供給它的參數。如果沒有在Python 3中刪除多餘%S將打印以下錯誤:

TypeError: not enough arguments for format string

此外,在Python 3,你可以使用新的字符串格式化語法:

print ("{}'s hand: {}, {} : {}".format(name, faces[0], faces[1], value)) 

有時比更靈活用舊的方式插入字符串用%s,肯定有用的功能就知道了。

相關問題