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

发布于 2025-01-15 09:15:11 字数 3940 浏览 0 评论 0原文

我正在将许多 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 语句逐行进行调试表明事情正在按预期工作。因此,我对这里发生的事情有点迷失。

I'm converting many of my R programs to Python (a language I don't use on a day-to-day basis).

Here my program, which simulates a simple card game:

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: 
quot;,jackpot, "\n \n") 
      break
    else:
      print("\n Outcome: Sorry, you didn't select the Ace of Spades! Better luck next time! \n Payoff: 
quot;,(0.20 * payoff)) # 20% of ticket sales goes to winning ticket holder each week  

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

   cards = cards # reset deck

Appearently the bug is in the fill_envelopes line.

Here's the error returned by the interpreter:

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

It seems that the interpreter is trying to treat a string object as an integer. This was found while also encountering a TypeError.

Debugging using print statements line-by-line reveals things are working as expected. Thus, i'm a bit lost as to what's going on here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾听心声的旋律 2025-01-22 09:15:11

两件快速的事情:您可能只是复制/粘贴错误,但是您在循环之外有一个中断。如果您希望 if/else 位于循环中,请确保其缩进正确。另一件事,这是你的实际问题:你正在重用一个你想保持静态的变量。

第一次运行代码效果很好,但第二次就崩溃了。为什么?它与这行cards = cards[not cards in pick_envelope]有关。在这里,您用一张卡片替换 cards 列表,然后永远不会将其重新放入所有 52 张卡片的原始列表中。我怀疑这实际上是一个拼写错误,您的意思是该变量是单数card。如果这不是拼写错误,则需要在循环开始时将卡片列表重新定义为 52 张卡片的列表。否则,第二次循环时将出现 cards = 'someString',并且您将收到 ValueError

编辑:为了进一步澄清 - 您在 while 循环的第二次循环时收到此错误,因为您将 cards 列表更改为只有一张卡的字符串,而该卡恰好是之前的卡在上一次迭代中“选择”。您需要确保不更改原始的卡片列表,或者在循环开始时重新定义它。

Two quick things: You might have just copy/pasted wrong, but you have a break outside of a loop. If you want that if/else to be in the loop, make sure it is indented correctly. The other thing, which is your actual problem: you are reusing a variable you want to remain static.

Running your code the first time works just fine, but second time breaks. Why? It has to do with this line cards = cards[not cards in pick_envelope]. Here, you replace the cards list with a single card, and then never re-make it into the original list of all 52 cards. I suspect this is actually a typo and you meant for that variable to be the singular card. If this is NOT a typo, you need to re-define the cards list to be the list of 52 cards at the start of the loop. Otherwise, the second time around the loop will have cards = 'someString', and you will get the ValueError.

EDIT: For further clarification – you are getting this error on the second go around the while loop, because you change your cards list to a string that has only one card, which happens to be the card that was 'chosen' in the previous iteration. You need to either make sure you don't change the original cards list, or re-define it at the start of the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文