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: $",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

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.

## Advertisement

## Answer

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.