我该怎么打印呢? (二十一点)

时间:2016-10-20 16:55:57

标签: python printing blackjack

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)

1 个答案:

答案 0 :(得分:1)

我在这里假设您使用的是Python 3.x并收到此错误:

  

TypeError:%不支持的操作数类型:'NoneType'和'tuple'

您需要在打印括号内的%之后移动部件以避免错误。此外,您的打印件中有一个<!DOCTYPE html> <html> <head> <textarea rows="10" cols="50" maxlength = "20000" id = "textContent">Content</textarea> <style> table { width: 100%; border-collapse: collapse; } table, td, th { border: 1px solid black; padding: 5px; } th {text-align: left;} </style> </head> <body> <?php $q = intval($_GET['q']); $con = mysqli_connect('localhost','root','gwatchgwatch63','mdmmp'); if (!$con) { die('Could not connect: ' . mysqli_error($con)); } mysqli_select_db($con,"jvrs"); $sql="select objectID,objectTextLine from objecttextline where objectID = '".$q."' order by objectTextLineOrder"; echo "SQL::".$sql; $result = mysqli_query($con,$sql); $textContent[0] = " "; $textContent[1] = " "; $textContent[2] = " "; $textContent[3] = " "; $textContent[4] = " "; $textContent[5] = " "; $i=0; while($i < 6 and $row = mysqli_fetch_array($result)) { $textContent[$i] = $row['objectTextLine'] ; echo $textContent[$i]; } mysqli_close($con); ?> <button onclick={document.getElementById("textContent").value="<?php echo $textContent[0]; ?>";}>query</button> <button onclick={x=document.getElementById("textContent").value;alert(x);}>query1</button> </body> </html> 太多。删除它,然后打印正常:

%s
  经销商的手:王牌,8:19

     

球员的手:9,7:16

如您所见,print ("%s's hand: %s, %s : %s" % (name, faces[0], faces[1], value)) :s的数量应该等于您提供给它的参数。如果不删除额外的%s,它将在Python 3上打印以下错误:

  

TypeError:格式字符串

的参数不足

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

%s

它有时比使用%s的字符串插值的旧方式更灵活,并且当然有用的功能。