【问题标题】:Return is always giving me the same valueReturn 总是给我相同的价值
【发布时间】:2015-08-19 15:38:07
【问题描述】:

无论 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))

【问题讨论】:

  • rank == "J" or "Q" or "K" 会返回真,你想做rank in ["J" , "Q" , "K"]
  • 您确实应该努力将错误减少到尽可能少的代码。这会让你知道哪一行是不正确的。
  • 您还需要在分配值之前对排名进行排序。 A, J, J 应该是 21,而不是 31。J, A, A 应该是 12,而不是 22,但这有点复杂。

标签: python function printing return return-value


【解决方案1】:

你的问题之一是这一行:

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

就目前而言,这将总是评估为真(嗯,技术上是“Q”)。您应该将 rank 与所有变量进行比较:

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

或者更 Pythonic 的方式:

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

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

【讨论】:

  • 添加此更改后,总数现在始终评估为 0。
  • 这是因为您不是在比较排名,而是在比较“卡片”(整对)..请参阅下面的答案..
  • @mtomney 我已经更新了我的答案 - 正如 navneet35371 提到的,您正在将一个元组与一个字符串进行比较。
【解决方案2】:

你应该改变这个:

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

到这里:

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

请参阅operator precedence 了解更多信息。

【讨论】:

    【解决方案3】:

    正如人们所说,你的第一个 if 是不行的。

    另外,当我尝试运行您的代码时,我还注意到您不是在比较排名,而是在比较“卡片”(整对)

    您的代码工作如下:

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

    【讨论】:

      【解决方案4】:

      这段代码在这里工作:

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

      【讨论】:

      • 好的,我得到调用索引 [1] 以仅指定卡值本身。对我来说,上面的代码目前只返回手中两张牌的第一个值。我认为 for 循环遍历列表(或元组)中的每个项目。
      【解决方案5】:

      我认为问题在于:

      如果排名 == “J”或“Q”或“K”:

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

      如果 rank=="J" 或 rank=="Q" 或 rank=="K"...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-29
        • 2017-04-25
        • 2017-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-17
        相关资源
        最近更新 更多