def initDeck(n):
cards = []
for i in range(n):
cards.append(i + 1)
return cards
def cutDeck(deck):
decklength = len(deck)
if((decklength % 2) != 0): #odds
numTop = decklength//2
numBot = numTop + 1
else: #evens
numTop = decklength//2
numBot = decklength//2
bottomDeck = []
bottomDeck = deck[:(numBot)]
topDeck = []
topDeck = deck[(numBot):]
return topDeck, bottomDeck
def shuffle(topDeck, bottomDeck):
newDeck = []
numcards = (len(topDeck)) + (len(bottomDeck))
for g in range(numcards):
newDeck.append(bottomDeck[g])
newDeck.append(topDeck[g])
return newDeck
#--------MAIN--------
n = int(input("How many cards do you want to shuffle? "))
numshuffles = int(input("How many times would you like to shuffle the deck? "))
deck = initDeck(n)
topDeck,bottomDeck = cutDeck(deck)
print(bottomDeck, '\n', topDeck, sep="")
while(numshuffles > 0):
shuffledDeck = shuffle(topDeck, bottomDeck)
numshuffles += -1
print(shuffledDeck)
프로그램에서 원하는 카드의 수, 셔플을 원하는 횟수, 리플은 데크를 셔플합니다. 문제는 실행하려고 할 때 두 개의 입력을 받아서 두 개의 오류를 출력하는 것입니다.Python3 :리스트 인덱스가 범위를 벗어났습니다.
Traceback (most recent call last):
File "C:\etc", line 51, in <module>
shuffledDeck = shuffle(topDeck, bottomDeck)
File "C:\etc", line 35, in shuffle
newDeck.append(bottomDeck[g])
IndexError: list index out of range
잘 생겼고 나에게 의미가있어 무엇이 잘못되었는지 잘 모르겠습니다. 이것이 8am에 만기가되는 때 어떤 도움든지 아주 중대하게 평가 될 것입니다!
응답 해 주셔서 감사합니다! 문제를 해결하려면 어떻게해야합니까? '(len (topDeck)) + (len (bottomDeck))'뒤에'- 1'을 추가하려고 시도했으나 작동하지 않아 range (numcards - 1)에 추가하려했으나 어느 쪽이든 일하십시오. 감사! – Iounno
고마워요! 이제는 내가 섞을 때마다 똑같은 것을 출력하는 문제가 있습니다. 내'newDeck []'이'shuffle()'함수 내부에 문제가 있습니까? 'MAIN'으로 옮기면 목록이 계속 확장됩니다. 이 문제를 어떻게 해결할 수 있습니까? 도와 주셔서 감사합니다 btw! 내가 할 수 있다면 너에게 upvote 줄. – Iounno
'topDeck'과'bottomDeck'이 바뀌지 않았기 때문에. 'shuffledDeck'에서 새로운'topDeck'과'bottomDeck'을 만들 수 있습니다 만,'len (topDeck)'이후에 반복됩니다. 또한 shoudle에'random '모듈을 사용합니다. upvote 수없는 경우 게시 한 문제가 해결 된 이후 허용 된 것으로 표시 할 수 있습니다. –