working on an algorithm to deal 6 cards from a standard deck and compare the deals combinations to a target prime.The code bugs up due to the indexing of the Permutation array "Result".Any suggestions?
import random
cards=[1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,
10,11,,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13]
deal=[0,0,0,0,0,0]
for i in range(0,6):
deal[i]=cards.pop(random.randint(0,len(cards)-1))
print(deal)
Result = [[0 for x in range(3)] for x in range(256)]
Result[0][0]= deal[0]+deal[1]
Result[0][1]= deal[0]-deal[1]
Result[0][2]= deal[0]*deal[1]
Result[0][3]= deal[0]/deal[1]
for i in range(2,5):
for j in range(2^(2*j),256):
Result[0][j]= Result[0][j]+deal[i]
Result[1][j]= Result[0][j]-deal[i]
Result[2][j]= Result[0][j]*deal[i]
Result[3][j]= Result[0][j]/deal[i]
print(Result)
The error message is : Traceback (most recent call last): File "C:/Users/xxx/Desktop/Dealhit.py", line 16, in Result[0][3]= deal[0]/deal[1]
IndexError: list index out of range. Which creats a [4x1024] array of mostly empty values.
for j in range(len(Result-1)):
Should be
for j in range(len(result)-1):
Otherwise (result - 1)
will be exucuted before len(result)
. Resulting in TypeError: unsupported operand type(s) for -: 'list' and 'int'
Because i
is in range(5)
, i
can take the value 4, which, added to 2, gives 6, which is out of the bounds of deal
.
I would recommend replacing
range(5)
by
range(len(deal) - 2)
You'll have another problem after that, when you try to assign Result[1][j]
, because Result
contains only one element. You need to either create a full 2d matrix of the size you need, or append items dynamically.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.