2017-11-06 3 views
-1

나는 보드 게임 Onitama의 파이썬 버전을 쓰고 있어요, 나는 현재 카드 셔플하는 함수를 작성하려고 해요 :지수는

temp_deck = onitama_deck.deck 
print(temp_deck) 
print(len(onitama_deck.deck)) 
print(len(temp_deck)) 
for i in range(len(temp_deck)): 
    next_card = temp_deck[random.randrange(0, len(temp_deck))] 
    deck.deck[i] = next_card 
    temp_deck.remove(next_card) 
print(onitama_deck.deck) 

모두를 onitama_deck.deck 및 temp_deck 목록이 있지만, 나는 1-10의 값을 포함하는 데크로 프로그램을 실행할 때, 나는 다음과 같은 출력을 얻을 : 내가 너무 큰 이유를 궁금 처음

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
10 
10 
Traceback (most recent call last): 
    File "onitama.py", line 33, in <module> 
    deck.shuffle() 
    File "onitama.py", line 25, in shuffle 
    onitama_deck.deck[i] = next_card 
IndexError: list assignment index out of range 

를, 그래서 두 목록을 모두 인쇄 해 보았습니다.

iteration: 1 
temp_deck: [2, 3, 4, 5, 6, 7, 8, 9, 10] 
deck: [2, 3, 4, 5, 6, 7, 8, 9, 10] 

iteration: 2 
temp_deck: [2, 4, 5, 6, 7, 8, 9, 10] 
deck: [2, 4, 5, 6, 7, 8, 9, 10] 

iteration: 3 
temp_deck: [2, 4, 6, 7, 8, 9, 10] 
deck: [2, 4, 6, 7, 8, 9, 10] 

iteration: 4 
temp_deck: [2, 4, 6, 8, 9, 10] 
deck: [2, 4, 6, 8, 9, 10] 

iteration: 5 
temp_deck: [2, 4, 6, 8, 10] 
deck: [2, 4, 6, 8, 10] 

및 다음 I 범위 오차 중 인덱스 얻을 : 어떤 이유로, 각각 최저 홀수 temp_deck에서 제거하고 onitama_deck.deck 같이, temp_deck 동일하게 설정된다 반복. 어쨌든 내 논리의 결함을 설명 할 수 있습니까? 도와 주셔서 감사합니다.

+1

오류가 발생한 코드를 표시하지 않으면 어떻게 도움이 될 것으로 생각하십니까? – Julien

+0

왜 random.shuffle()을 사용하지 않는 것이 좋을까요? –

답변

2

문제는 temp_deck 갑판의 복사본이되지 않는 것입니다,하지만 같은 갑판에 대한 참조 :

:

temp_deck = onitama_deck.deck 

복사본을 만들려면, 당신은이를 변경할 수 있습니다

temp_deck = list(onitama_deck.deck)