回报总是给我相同的价值

时间:2015-08-19 15:38:08

标签: python function printing return return-value

无论user_hand中有哪些卡,我的返回值总是打印出20个。有什么理由为什么?

suits = ["Heart", "Diamond", "Spade", "Club"]
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']

deck = [(suit, rank) for rank in ranks for suit in suits]

random.shuffle(deck,random.random)

user_hand = []
dealer_hand = []

user_hand.append(deck.pop())

dealer_hand.append(deck.pop())

user_hand.append(deck.pop())

dealer_hand.append(deck.pop())

def handtotal (hand):

    total = 0

    for rank in hand:

        if rank == "J" or "Q" or "K":

            total += 10

        elif rank == 'A' and total < 11:

            total += 11

        elif rank == 'A' and total >= 11:

            total += 1

        elif rank == '2':

            total += 2

        elif rank == '3':

            total += 3

        elif rank == '4':

            total += 4

        elif rank == '5':

            total += 5

        elif rank == '6':

            total += 6

        elif rank == '7':

            total += 7

        elif rank == '8':

            total += 8

        elif rank == '9':

            total += 9

    return total


print ("Your current hand is {}".format(user_hand))

print ("This provides you with a total of:")

print (handtotal(user_hand))

5 个答案:

答案 0 :(得分:1)

你的一个问题是这一行:

if rank == "J" or "Q" or "K"

就目前而言,这将始终评估为真(嗯,技术上&#34; Q&#34;)。您应该将rank与所有变量进行比较:

if rank == "J" or rank == "Q" or rank == "K"

或更多Pythonic方式:

if rank in ["J", "Q", "K"] 

此外,您已将user_hand传递给handtotal()。由于user_hand的每个元素都是元组,因此您需要与rank[1]的每个元素(例如user_hand)的第二个元素进行比较。

答案 1 :(得分:0)

你应该改变这个:

if rank == "J" or "Q" or "K":

对此:

if rank in ["J", "Q", "K"]:

有关详细信息,请参阅operator precedence

答案 2 :(得分:-1)

正如人们所说,你的第一个如果不好。

另外,当我试图运行你的代码时,我也注意到你没有比较排名,而是'卡'(整个对)

您的代码如下:

import random
suits = ["Heart", "Diamond", "Spade", "Club"]
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']

deck = [(suit, rank) for rank in ranks for suit in suits]

random.shuffle(deck,random.random)

user_hand = []
dealer_hand = []

user_hand.append(deck.pop())

dealer_hand.append(deck.pop())

user_hand.append(deck.pop())

dealer_hand.append(deck.pop())

def handtotal (hand):

    total = 0

    for rank in hand:
        if rank[1] == "J" or rank[1] == "Q" or rank[1] == "K":
            total += 10
        elif rank[1] == 'A' and total < 11:
            total += 11
        elif rank[1] == 'A' and total >= 11:
            total += 1
        elif rank[1] == '2':
            total += 2
        elif rank[1] == '3':
            total += 3
        elif rank[1] == '4':
            total += 4
        elif rank[1] == '5':
            total += 5
        elif rank[1] == '6':
            total += 6
        elif rank[1] == '7':
            total += 7
        elif rank[1] == '8':
            total += 8
        elif rank[1] == '9':
            total += 9
    return total


print ("Your current hand is {}".format(user_hand))

print ("This provides you with a total of:")

print (handtotal(user_hand))

答案 3 :(得分:-1)

此代码适用于此处:

import random
suits = ["Heart", "Diamond", "Spade", "Club"]
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']

deck = [(suit, rank) for rank in ranks for suit in suits]

random.shuffle(deck,random.random)

user_hand = []
dealer_hand = []

user_hand.append(deck.pop())

dealer_hand.append(deck.pop())

user_hand.append(deck.pop())

dealer_hand.append(deck.pop())

def handtotal (hand):

    total = 0

    for rank in hand:
    rank = rank[1]
        if rank in ["J", "Q","K"]:

            total += 10

        elif rank == 'A' and total < 11:

            total += 11

        elif rank == 'A' and total >= 11:

            total += 1

        elif rank == '2':

            total += 2

        elif rank == '3':

            total += 3

        elif rank == '4':

            total += 4

        elif rank == '5':

            total += 5

        elif rank == '6':

            total += 6

        elif rank == '7':

            total += 7

        elif rank == '8':

            total += 8

        elif rank == '9':

            total += 9

    return total


print ("Your current hand is {}".format(user_hand))

print ("This provides you with a total of:")

print (handtotal(user_hand))

答案 4 :(得分:-2)

我认为问题在于:

如果排名==&#34; J&#34;或&#34; Q&#34;或者&#34; K&#34;:

我知道你可以用python做很多奇怪的事情,但我很确定这句话应该是

如果排名==&#34; J&#34;或排名==&#34; Q&#34;或排名==&#34; K&#34; ...