Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This code won\'t run what\'s the problem? dCardNames = [\'2\',\'3\',\'4\',\'5\',

ID: 3712337 • Letter: T

Question

This code won't run what's the problem?

dCardNames = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
dCardValues = ['2','3','4','5','6','7','8','9','10','11','12','13','14']
dSuits = ["Clubs","Spades","Diamonds","Hearts"]
# Build a two dimensional deck with Cards suits and values.
aCards = [['' for i in range(52)] for j in range(3)]
i = 0
n = 0
while i < 13:
aCards[0][i] = dCardNames[i]
aCards[0][i + 13] = dCardNames[i]
aCards[0][i + 26] = dCardNames[i]
aCards[0][i + 39] = dCardNames[i]
aCards[1][i] = dSuits[0]
aCards[1][i + 13] = dSuits[1]
aCards[1][i + 26] = dSuits[2]
aCards[1][i + 39] = dSuits[3]
aCards[2][i] = dCardValues[i]
aCards[2][i + 13] = dCardValues[i]
aCards[2][i + 26] = dCardValues[i]
aCards[2][i + 39] = dCardValues[i]
i = i + 1
i = 0
while i < 52:
print (aCards[0][i], " ", aCards[1][i], " ", aCards[2][i])
i = i + 1

from random import randint
# To shuffle 52 cards
i=51
while i>0:
newindex=randint(0,i)
name=aCards[0][i]
value=aCards[1][i]
suit=aCards[2][i]
aCards[0][i]=aCards[0][newindex]
aCards[1][i]=aCards[1][newindex]
aCards[2][i]=aCards[2][newindex]
aCards[0][newindex]=name
aCards[1][newindex]=value
aCards[2][newindex]=suit
i=i-1
i=0
# Shuffled list
print('After shuffling:')
while i < 52:
print (aCards[0][i], " ", aCards[1][i], " ", aCards[2][i])
i = i + 1
# Hand 1: Top two cards of shuffles deck
print('Hand 1:')
print (aCards[0][51], " ", aCards[1][51], " ", aCards[2][51])
print (aCards[0][50], " ", aCards[1][50], " ", aCards[2][50])

# Hand 2:
print('Hand 2:')
print (aCards[0][49], " ", aCards[1][49], " ", aCards[2][49])
print (aCards[0][51], " ", aCards[1][48], " ", aCards[2][48])

Explanation / Answer

Actually in the first while loop which is started from 8th line has an error and the error is that at the end of the loop you have reinitialised the value of i to 0. which makes an infinite loop and hence the code is not showing any output. I have rectified the code and you can run this code. this is working.

dCardNames = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']

dCardValues = ['2','3','4','5','6','7','8','9','10','11','12','13','14']

dSuits = ["Clubs","Spades","Diamonds","Hearts"]

# Build a two dimensional deck with Cards suits and values.

aCards = [['' for i in range(52)] for j in range(3)]

i = 0

n = 0

while (i < 13):

aCards[0][i] = dCardNames[i]

aCards[0][i + 13] = dCardNames[i]

aCards[0][i + 26] = dCardNames[i]

aCards[0][i + 39] = dCardNames[i]

aCards[1][i] = dSuits[0]

aCards[1][i + 13] = dSuits[1]

aCards[1][i + 26] = dSuits[2]

aCards[1][i + 39] = dSuits[3]

aCards[2][i] = dCardValues[i]

aCards[2][i + 13] = dCardValues[i]

aCards[2][i + 26] = dCardValues[i]

aCards[2][i + 39] = dCardValues[i]

i = i + 1

while (i < 52):

print (aCards[0][i], " ", aCards[1][i], " ", aCards[2][i])

i = i + 1

from random import randint

# To shuffle 52 cards

i=51

while (i>0):

newindex=randint(0,i)

name=aCards[0][i]

value=aCards[1][i]

suit=aCards[2][i]

aCards[0][i]=aCards[0][newindex]

aCards[1][i]=aCards[1][newindex]

aCards[2][i]=aCards[2][newindex]

aCards[0][newindex]=name

aCards[1][newindex]=value

aCards[2][newindex]=suit

i=i-1

i=0

# Shuffled list

print('After shuffling:')

while (i < 52):

print (aCards[0][i], " ", aCards[1][i], " ", aCards[2][i])

i = i + 1

# Hand 1: Top two cards of shuffles deck

print('Hand 1:')

print (aCards[0][51], " ", aCards[1][51], " ", aCards[2][51])

print (aCards[0][50], " ", aCards[1][50], " ", aCards[2][50])

# Hand 2:

print('Hand 2:')

print (aCards[0][49], " ", aCards[1][49], " ", aCards[2][49])

print (aCards[0][51], " ", aCards[1][48], " ", aCards[2][48])