【问题标题】:Random card game随机纸牌游戏
【发布时间】:2013-04-03 13:29:12
【问题描述】:

我目前正在做一个随机纸牌游戏项目,程序应该向用户显示 5 张随机纸牌,(第一个问题):我不知道如何随机排列字母列表,这是我的代码:

def play():
    hand = ["A","2","3","4","5","6","7","8","9","T","J","Q","K"]

    for i in range(len(hand)):
        card = random.choice[{hand},4]   

    print "User >>>> ",card
    return card

第二个问题:如果用户想改变卡片的位置。用户应输入编号。的位置变化,那么程序应该随机改变卡。例如:AJ891,用户输入:1,--> A2891。我应该怎么办?这是我的原始代码,但它不起作用

def ask_pos():
    pos_change = raw_input("From which position (and on) would you want to change? (0 to 4)? ")
    while not (pos_change.isdigit()):
        print "Your input must be an integer number"
        pos_change = raw_input("From which position (and on) would you want to change? (0 to 4)? ")
        if (pos_change > 4) :
            print "Sorry the value has to be between 0 and 4, please re-type"
            pos_change = raw_input("From which position (and on) would you want to change? (0 to 4)? ")
    return pos_change

    hand = ["A","2","3","4","5","6","7","8","9","T","J","Q","K"]

    for i in range(len(hand)):
        card = random.choice[{hand},4]
        new = random.choice[{hand},1]

            for i in range(len(card)):
                if (card[i] == pos_change):
                    card = card + new

    return card

【问题讨论】:

  • random.choice[{hand},4] 这是你编造的,还是有人告诉你在愚人节使用这个语法?这条线有很多问题,我什至不知道从哪里开始
  • 这段代码不会运行;所以请粘贴运行代码。
  • random.sample(hand,5),随机返回一个包含 5 件手牌的列表。 (随机导入,或者从随机导入样本,但是然后 -> sample(hand,5)

标签: python random


【解决方案1】:

1)

random.choice[{hand},4]

这行不通,语法错误。选择也无济于事,样品就是你想要的:

random.sample(hand, 5)

2)

pick = random.sample(hand, 5)

change = 2  # Entered by the user

pick[change] = random.choice([x for x in hand if x not in pick])

【讨论】:

    【解决方案2】:

    回答你的第一个问题:

    import random
    hands = ["A","2","3","4","5","6","7","8","9","T","J","Q","K"]
    def play():
        cards = random.sample(hands,5)
        print "User >>>> ", cards
        return cards
    

    random.choice[{hand},4] 应该会导致语法错误。首先,调用你使用括号()而不是括号[]的函数。另外,我不明白你为什么需要在手边放大括号 {},因为它已经是一个列表,所以不需要做任何事情。

    我重写了你的第二个问题:

    def ask_pos(hand):
        while 1:
            pos_change = raw_input("From which position (and on) would you want to change? (0 to 4)? ")
            if int(pos_change) < 0 or int(pos_change) > 4:
                continue
            else:
                break
        hand[int(pos_change)] = random.choice(hands)
        return hand
    

    运行时:

    >>> myhand = play()
    User >>>>  ['6', '8', 'A', '9', '4']
    
    >>> ask_pos(myhand)
    From which position (and on) would you want to change? (0 to 4)? 0
    ['Q', '8', 'A', '9', '4']
    

    【讨论】:

    • 首先,感谢您的帮助。我在我的程序中尝试了你的代码,但出现错误,它说: NameError: global name 'hand' is not defined
    • 你想做什么?即错误来自哪里
    • 我卡在代码的第二部分,程序无法改变卡片的位置。
    • 你在输入参数吗?要调用该函数,请执行ask_pos(yourlist)。确保yourlist 是一个列表,就像我在示例中显示的那样
    • 是的,我做了,但是当我运行程序时,有一个错误说 TypeError: ask_pos() 只需要 1 个参数(给定 0)
    【解决方案3】:

    你可以使用random.randrange(number),然后拉出那个位置的索引。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多