在 python 中使用 pop 时如何解决索引错误?
How to solve index error while using pop in python?
这是我遇到的错误(图中)
从一副牌中洗出13张牌,分配给4位玩家,洗牌后检查游戏规则,如果不符合规则则重新洗牌。
当重新洗牌发生时,它显示从空列表弹出错误
import random
suites=['Hearts', 'Diamonds', 'Clubs', 'Spades']
values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
deck=[]
class cards:
def __init__(self,value,suit,priority):
self.suit=suit
self.value=value
self.priority=priority
def __str__(self):
return "{} of {}".format(self.value,self.suit)
class player:
def __init__(self,name):
self.name=name
self.point=0
self.hand=[]
def show_cards(self):
for a in self.hand:
print(a)
def un_deck():
for i in suites:
p=1
for j in values:
if i =="Spades":
pri=p*15
p+=1
else:
pri=p
p+=1
deck.append(cards(j,i,pri))
def shuffle() :
random.shuffle(deck)
return deck
p=[player("Player 1"),player("Player 2"),player("Player 3"),player("Player 4")]
def distribue_cards():
card=shuffle()
n=51
while n>=0:
print("-----------------------length=",len(card),"---------------------")
if n%4==0:
p[0].hand.append(card.pop(n))
elif n%4==1:
p[1].hand.append(card.pop(n))
elif n%4==2:
p[2].hand.append(card.pop(n))
elif n%4==3:
p[3].hand.append(card.pop(n))
n-=1
# del card
# del n
check()
def check() :
for i in p:
ace,heart,diamond,spade,club=0,0,0,0,0
for j in i.hand:
if j.value=="A":
ace+=1
if j.suit=="Hearts":
heart+=1
elif j.suit=="Diamonds":
diamond+=1
elif j.suit=="Clubs":
club+=1
else:
spade+=1
if (ace>=3 or heart>=7 or diamond>=7 or club>=7 or spade>=7 or heart==0 or diamond==0 or club==0 or spade==0):
distribue_cards()
break
#main
un_deck()
distribue_cards()
for b in p:
print("player name:",b.name)
b.show_cards()
Error message(length in output is for testing purpose)
错误:
card
被清空,当您在函数内部调用 check()
时,如果满足特定条件,您将调用 distribue_cards()
。这是你的错误,因为 card
已经清空并且在空列表中调用 pop()
会产生错误。
此外,你没有清除分配给不这样做的玩家的卡牌,会在他们手中堆积很多卡牌,导致无限循环(由于你给定的条件ace>=3 or heart>=7 or diamond>=7 or club>=7 or spade>=7 or heart==0 or diamond==0 or club==0 or spade==0)
).
代码:
import random
suites=['Hearts', 'Diamonds', 'Clubs', 'Spades']
values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
deck=[]
class cards:
def __init__(self,value,suit,priority):
self.suit=suit
self.value=value
self.priority=priority
def __str__(self):
return "{} of {}".format(self.value,self.suit)
class player:
def __init__(self,name):
self.name=name
self.point=0
self.hand=[]
def show_cards(self):
for a in self.hand:
print(a)
def un_deck():
for i in suites:
p=1
for j in values:
if i =="Spades":
pri=p*15
p+=1
else:
pri=p
p+=1
deck.append(cards(j,i,pri))
def shuffle() :
random.shuffle(deck)
return deck
p=[player("Player 1"),player("Player 2"),player("Player 3"),player("Player 4")]
def distribute_cards():
card=shuffle()
n=51
_0=0
_1=0
_2=0
_3=0
while n>=0:
print("-----------------------length=",len(card),"---------------------")
if n%4==0:
_0 +=1
p[0].hand.append(card.pop(n))
elif n%4==1:
_1 += 1
p[1].hand.append(card.pop(n))
elif n%4==2:
_2 +=1
p[2].hand.append(card.pop(n))
elif n%4==3:
_3 += 1
p[3].hand.append(card.pop(n))
n-=1
# del card
# del n
check()
def check() :
for i in p:
ace,heart,diamond,spade,club=0,0,0,0,0
for j in i.hand:
if j.value=="A":
ace+=1
if j.suit=="Hearts":
heart+=1
elif j.suit=="Diamonds":
diamond+=1
elif j.suit=="Clubs":
club+=1
else:
spade+=1
if (ace>=3 or heart>=7 or diamond>=7 or club>=7 or spade>=7 or heart==0 or diamond==0 or club==0 or spade==0):
#if the condition is True then:
un_deck() # create new deck
for i in range(4):
p[i].hand=[] # Clear players' cards
distribute_cards() # Then only call distribute_cards
break
#main
un_deck()
distribute_cards()
for b in p:
print("player name:",b.name)
b.show_cards()
这是我遇到的错误(图中)
从一副牌中洗出13张牌,分配给4位玩家,洗牌后检查游戏规则,如果不符合规则则重新洗牌。 当重新洗牌发生时,它显示从空列表弹出错误
import random
suites=['Hearts', 'Diamonds', 'Clubs', 'Spades']
values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
deck=[]
class cards:
def __init__(self,value,suit,priority):
self.suit=suit
self.value=value
self.priority=priority
def __str__(self):
return "{} of {}".format(self.value,self.suit)
class player:
def __init__(self,name):
self.name=name
self.point=0
self.hand=[]
def show_cards(self):
for a in self.hand:
print(a)
def un_deck():
for i in suites:
p=1
for j in values:
if i =="Spades":
pri=p*15
p+=1
else:
pri=p
p+=1
deck.append(cards(j,i,pri))
def shuffle() :
random.shuffle(deck)
return deck
p=[player("Player 1"),player("Player 2"),player("Player 3"),player("Player 4")]
def distribue_cards():
card=shuffle()
n=51
while n>=0:
print("-----------------------length=",len(card),"---------------------")
if n%4==0:
p[0].hand.append(card.pop(n))
elif n%4==1:
p[1].hand.append(card.pop(n))
elif n%4==2:
p[2].hand.append(card.pop(n))
elif n%4==3:
p[3].hand.append(card.pop(n))
n-=1
# del card
# del n
check()
def check() :
for i in p:
ace,heart,diamond,spade,club=0,0,0,0,0
for j in i.hand:
if j.value=="A":
ace+=1
if j.suit=="Hearts":
heart+=1
elif j.suit=="Diamonds":
diamond+=1
elif j.suit=="Clubs":
club+=1
else:
spade+=1
if (ace>=3 or heart>=7 or diamond>=7 or club>=7 or spade>=7 or heart==0 or diamond==0 or club==0 or spade==0):
distribue_cards()
break
#main
un_deck()
distribue_cards()
for b in p:
print("player name:",b.name)
b.show_cards()
Error message(length in output is for testing purpose)
错误:
card
被清空,当您在函数内部调用check()
时,如果满足特定条件,您将调用distribue_cards()
。这是你的错误,因为card
已经清空并且在空列表中调用pop()
会产生错误。此外,你没有清除分配给不这样做的玩家的卡牌,会在他们手中堆积很多卡牌,导致无限循环(由于你给定的条件
ace>=3 or heart>=7 or diamond>=7 or club>=7 or spade>=7 or heart==0 or diamond==0 or club==0 or spade==0)
).
代码:
import random
suites=['Hearts', 'Diamonds', 'Clubs', 'Spades']
values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
deck=[]
class cards:
def __init__(self,value,suit,priority):
self.suit=suit
self.value=value
self.priority=priority
def __str__(self):
return "{} of {}".format(self.value,self.suit)
class player:
def __init__(self,name):
self.name=name
self.point=0
self.hand=[]
def show_cards(self):
for a in self.hand:
print(a)
def un_deck():
for i in suites:
p=1
for j in values:
if i =="Spades":
pri=p*15
p+=1
else:
pri=p
p+=1
deck.append(cards(j,i,pri))
def shuffle() :
random.shuffle(deck)
return deck
p=[player("Player 1"),player("Player 2"),player("Player 3"),player("Player 4")]
def distribute_cards():
card=shuffle()
n=51
_0=0
_1=0
_2=0
_3=0
while n>=0:
print("-----------------------length=",len(card),"---------------------")
if n%4==0:
_0 +=1
p[0].hand.append(card.pop(n))
elif n%4==1:
_1 += 1
p[1].hand.append(card.pop(n))
elif n%4==2:
_2 +=1
p[2].hand.append(card.pop(n))
elif n%4==3:
_3 += 1
p[3].hand.append(card.pop(n))
n-=1
# del card
# del n
check()
def check() :
for i in p:
ace,heart,diamond,spade,club=0,0,0,0,0
for j in i.hand:
if j.value=="A":
ace+=1
if j.suit=="Hearts":
heart+=1
elif j.suit=="Diamonds":
diamond+=1
elif j.suit=="Clubs":
club+=1
else:
spade+=1
if (ace>=3 or heart>=7 or diamond>=7 or club>=7 or spade>=7 or heart==0 or diamond==0 or club==0 or spade==0):
#if the condition is True then:
un_deck() # create new deck
for i in range(4):
p[i].hand=[] # Clear players' cards
distribute_cards() # Then only call distribute_cards
break
#main
un_deck()
distribute_cards()
for b in p:
print("player name:",b.name)
b.show_cards()