Python 程序中的奇怪错误:`str() 不能解释为整数

Strange bugs in Python program: `str() cannot be interpreted as integer

我正在将我的许多 R 程序转换为 Python(一种我日常不使用的语言)。

这是我的程序,它模拟了一个简单的纸牌游戏:

cards = ["Ace of Clubs",
    "Ace of Diamonds",
    "Ace of Hearts",
    "Ace of Spades",
    "2 of Clubs",
    "2 of Diamonds",
    "2 of Hearts",
    "2 of Spades",
    "3 of Clubs",
    "3 of Diamonds",
    "3 of Hearts",
    "3 of Spades",
    "4 of Clubs",
    "4 of Diamonds",
    "4 of Hearts",
    "4 of Spades",
    "5 of Clubs",
    "5 of Diamonds",
    "5 of Hearts",
    "5 of Spades",
    "6 of Clubs",
    "6 of Diamonds",
    "6 of Hearts",
    "6 of Spades",
    "7 of Clubs",
    "7 of Diamonds",
    "7 of Hearts",
    "7 of Spades",
    "8 of Clubs",
    "8 of Diamonds",
    "8 of Hearts",
    "8 of Spades",
    "9 of Clubs",
    "9 of Diamonds",
    "9 of Hearts",
    "9 of Spades",
    "10 of Clubs",
    "10 of Diamonds",
    "10 of Hearts",
    "10 of Spades",
    "Jack of Clubs",
    "Jack of Diamonds",
    "Jack of Hearts",
    "Jack of Spades",
    "King of Clubs",
    "King of Diamonds",
    "King of Hearts",
    "King of Spades",
    "Queen of Clubs",
    "Queen of Diamonds",
    "Queen of Hearts",
    "Queen of Spades"]

ticket_price = 5 # price per ticket
max_num_tickets = np.random.randint(1, 1000, 1) # maximum number of tickets sold

week = 0 # initialize counter
payoff = 0 # initialize weekly winnings
jackpot = 0 # initialize progressive jackpot

while(week < 52):
    week += 1 # increment counter
    tickets = np.random.randint(max_num_tickets + 1, size = 1) # random number of tickets sold
    fill_envelopes = np.random.choice(cards, size = len(cards), replace = False) # assign cards to envelopes
    pick_ticket = np.random.randint(tickets + 1, size = 1) # select random ticket
    pick_envelope = np.random.choice(fill_envelopes, size = 1) # choose random envelope
    cards = cards[not cards in pick_envelope] # remove selected card
    payoff = tickets * ticket_price # ticket sales
    jackpot = jackpot + (0.30 * payoff) # update weekly winnings; 30% of ticket sales goes into jackpot

    print("\n Week: ", week,
        "\n Ticket number: ", pick_ticket,
        "\n Card selected: ", pick_envelope)

    if ("Ace of Spades" in pick_envelope):
      print("\n Outcome: Congratulations, you've selected the Ace of Spades! You've won the progressive jackpot! \n Jackpot: $",jackpot, "\n \n") 
      break
    else:
      print("\n Outcome: Sorry, you didn't select the Ace of Spades! Better luck next time! \n Payoff: $",(0.20 * payoff)) # 20% of ticket sales goes to winning ticket holder each week  

   print("\n Proceeds donated to charity: $",(0.50 * payoff), "\n \n") # 50% of all ticket sales goes to charity

   cards = cards # reset deck

显然错误在 fill_envelopes 行。

这是解释器返回的错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
mtrand.pyx in numpy.random.mtrand.RandomState.choice()

TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-79-65a112fc8498> in <module>()
 62     week += 1 # increment counter
 63     tickets = np.random.randint(max_num_tickets + 1, size = 1) # random number of tickets sold
---> 64     fill_envelopes = np.random.choice(cards, size = len(cards), replace = False) # assign cards to envelopes
 65     pick_ticket = np.random.randint(tickets + 1, size = 1) # select random ticket
 66     pick_envelope = np.random.choice(fill_envelopes, size = 1) # choose random envelope

mtrand.pyx in numpy.random.mtrand.RandomState.choice()

ValueError: a must be 1-dimensional or an integer

解释器似乎试图将字符串对象视为整数。这是在遇到TypeError.

时发现的

使用 print 语句逐行调试显示一切正常。因此,我对这里发生的事情有点迷茫。

库 numpy 的错误是这样的:

你可以查一下https://numpy.org/doc/stable/reference/random/generated/numpy.random.choice.html

两件小事:您可能只是 copy/pasted 错了,但您在循环之外有一个中断。如果您希望 if/else 在循环中,请确保其缩进正确。另一件事,这是你的实际问题:你正在重用一个你想保持静态的变量。

运行 您的代码第一次运行正常,但第二次出现故障。为什么?跟这条线cards = cards[not cards in pick_envelope]有关。在这里,您将 cards 列表替换为一张卡片,然后永远不会将其 re-make 放入所有 52 张卡片的原始列表中。我怀疑这实际上是一个拼写错误,您的意思是该变量是单数 card。如果这不是拼写错误,您需要 re-define 卡片列表成为循环开始时的 52 张卡片列表。否则,第二次循环将有 cards = 'someString',你将得到 ValueError.

编辑:为了进一步说明——你在第二次绕过 while 循环时遇到了这个错误,因为你将 cards 列表更改为只有一张卡片的字符串,恰好是卡片在上一次迭代中是 'chosen'。您需要确保不更改原始 cards 列表,或者在循环开始时 re-define 它。