我正在將我的許多 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使用陳述句逐行除錯表明事情正在按預期作業。因此,我對這里發生的事情有點迷茫。
uj5u.com熱心網友回復:
兩件快速的事情:您可能只是復制/粘貼錯誤,但是您在回圈之外有一個中斷。如果您希望它if/else在回圈中,請確保它正確縮進。另一件事是你的實際問題:你正在重用一個你想保持靜態的變數。
第一次運行你的代碼就可以了,但第二次就中斷了。為什么?它與這條線有關cards = cards[not cards in pick_envelope]。在這里,您將cards串列替換為一張卡片,然后再將其重新放入所有 52 張卡片的原始串列中。我懷疑這實際上是一個錯字,您的意思是該變數是單數card。如果這不是錯字,您需要將卡片串列重新定義為回圈開始時的 52 張卡片串列。否則,第二次回圈將有cards = 'someString',您將獲得ValueError.
編輯:為了進一步澄清 - 您在第二次回圈時遇到此錯誤,因為您將cards串列更改為只有一張卡的字串,這恰好是在前一次迭代中“選擇”的卡。您需要確保不更改原始cards串列,或者在回圈開始時重新定義它。
uj5u.com熱心網友回復:
對于庫 numpy,錯誤是這樣的:

你可以查一下https://numpy.org/doc/stable/reference/random/generated/numpy.random.choice.html 我
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/446197.html
上一篇:很難理解Scala的基礎知識
