当我运行以下代码时,我收到以下错误消息:
IndexError:列出超出范围的索引
是什么原因导致的?
cardChosen = deck[random.randint(0, len(deck))]
应该在每一轮都保持最大射程的下降,但程序说我超出了射程。
import random
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] * 4
scorePlayer1 = 0
scorePlayer2 = 0
player1 = input("Player 1: ")
player2 = input("Player 2: ")
while len(deck) > 0:
def player_turn(player_name, deck):
deck = deck
print(len(deck))
cardChosen = deck[random.randint(0, len(deck))]
deck.remove(cardChosen)
print(player_name + " chose " + str(cardChosen))
return(cardChosen)
a = player_turn(player1, deck)
b = player_turn(player2, deck)
if a > b:
scorePlayer1 += 2
print(player1 + " wins this round:)")
elif b > a:
scorePlayer2 += 2
print(player2 + " wins this round :)")
elif a == b:
print("War!")
print(scorePlayer1)
print(scorePlayer2)
else:
print("Game Over")
if scorePlayer1 > scorePlayer2:
print(player1 + " has won the game:)")
elif scorePlayer2 > scorePlayer1:
print(player2 + " has won the game:)")
elif scorePlayer1 == scorePlayer2:
print("There has been a tie, both players won:)")
您获取的随机项目1太远。
当列表的长度为1并且您只剩下一个选择时,cardChosen = deck[random.randint(0, len(deck))]
相当于不存在的cardChosen = deck[0]
或deck[1]
。
您要使用random.randint(0, len(deck)-1)
。
编辑:始终检查您使用的库的文档。例如,内置的range(a, b)
不包括b
,而random.randint(a, b)
包括。
我鼓励您看看代码的这种格式。一开始这可能看起来有点令人生畏,但这只是因为我放了太多#笔记。
这是带有纸币的版本
’’’
import random
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] * 4
#>>> suggestion below
length = len(deck)
round = 1
scorePlayer1 = 0
scorePlayer2 = 0
player1 = input("Player 1: ")
player2 = input("Player 2: ")
print("Round: {}".format(round)) #>>> used for user clarification
# >>> Removed "while" statement, and kept the
#main player turn function as its own stand alone function
def player_turn(player_name, length):
#deck = deck >>>Line not needed
#print(len(deck)) >>> removed
#cardChosen = deck[random.randint(0, len(deck))] >>>solution below
cardChosen = random.choice(deck)
deck.remove(cardChosen)
#suggestion below
length -= 1
#print(player_name + " chose " + str(cardChosen)) >>>suggestion below
print("{} drew the card: {}".format(player_name, cardChosen))
#return(cardChosen) >>> suggestion below
return cardChosen, length
#as long as the length of the deck (which is updated
#everytime the function is called) is greater than zero
while length > 0:
round += 1#>>> used for user clarification
#set the variables cardChosen and length to equal
#whatever the output of the player turn function (with
#2 argument given) returns
cardChosen, length = player_turn(player1, length)
#then set the players score to equal the card drawn
scorePlayer1 += cardChosen
player1_hand = cardChosen
#afterwards repeat the same for player 2
cardChosen, length = player_turn(player2, length)
scorePlayer2 += cardChosen
player2_hand = cardChosen
#>>>replace "a" and "b" with better, readable variables
#if a > b:
if scorePlayer1 > scorePlayer2:
scorePlayer1 += 2
#print(player1 + " wins this round:)") >>> use the format method, highly recommended. suggestion below
print("{} won this round".format(player1))
print("{}'s score: {}".format(player1, scorePlayer1))
print("{}'s score: {}".format(player2, scorePlayer2))
print("nn") #>>>adds a space between rounds
elif scorePlayer2 > scorePlayer1:
scorePlayer2 += 2
#print(player2 + " wins this round :)") >>>suggestion below
print("{} won this round".format(player2))
print("{}'s score: {}".format(player1, scorePlayer1))
print("{}'s score: {}".format(player2, scorePlayer2))
print("nn") # >>> adds a space between rounds
if player1_hand == player2_hand:
#print("War!") >>> suggestiom below
print("WAAAAAAAR!!!") #>>>How will anyone take you seriously if you dont scream!
print("{}'s score: {}".format(player1, scorePlayer1))
print("{}'s score: {}".format(player2, scorePlayer2))
print("nn") # >>> adds a space between rounds
#else: >>> unneeded "else" statement since the "while" loop automatically
#stops once there are no more cards in the deck
#add a pause so the viewer can comprehend what the heck just happened
print("Number of cards left: {}".format(length))
pause = input("Press ENTER to continue...")
print("============================nn") #>>> adds a space
print("Round: {}".format(round))
print("Game Over")
if scorePlayer1 > scorePlayer2:
#print(player1 + " has won the game:)") >>> suggestion below
print("{} has won the game".format(player1))
elif scorePlayer2 > scorePlayer1:
#print(player2 + " has won the game:)") >>> suggestion below
print("{} has won the game".format(player2))
elif scorePlayer1 == scorePlayer2:
print("There has been a tie, both players won:)")
```
这是没有注释的版本。请注意,它的大小差不多。
import random
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] * 4
length = len(deck)
round = 1
scorePlayer1 = 0
scorePlayer2 = 0
player1 = input("Player 1: ")
player2 = input("Player 2: ")
print("Round: {}".format(round))
def player_turn(player_name, length):
cardChosen = random.choice(deck)
deck.remove(cardChosen)
length -= 1
print("{} drew the card: {}".format(player_name, cardChosen))
return cardChosen, length
while length > 0:
round += 1
cardChosen, length = player_turn(player1, length)
scorePlayer1 += cardChosen
player1_hand = cardChosen
cardChosen, length = player_turn(player2, length)
scorePlayer2 += cardChosen
player2_hand = cardChosen
if scorePlayer1 > scorePlayer2:
scorePlayer1 += 2
print("{} won this round".format(player1))
print("{}'s score: {}".format(player1, scorePlayer1))
print("{}'s score: {}".format(player2, scorePlayer2))
print("nn")
elif scorePlayer2 > scorePlayer1:
scorePlayer2 += 2
print("{} won this round".format(player2))
print("{}'s score: {}".format(player1, scorePlayer1))
print("{}'s score: {}".format(player2, scorePlayer2))
print("nn")
if player1_hand == player2_hand:
print("WAAAAAAAR!!!")
print("{}'s score: {}".format(player1, scorePlayer1))
print("{}'s score: {}".format(player2, scorePlayer2))
print("nn")
print("Number of cards left: {}".format(length))
pause = input("Press ENTER to continue...")
print("============================nn")
print("Round: {}".format(round))
print("Game Over")
if scorePlayer1 > scorePlayer2:
print("{} has won the game".format(player1))
elif scorePlayer2 > scorePlayer1:
print("{} has won the game".format(player2))
elif scorePlayer1 == scorePlayer2:
print("There has been a tie, both players won:)")
请知道我也是一个初学者,但我认为我们必须互相帮助,你知道吗?
因此,为了回答你的问题,像其他人所说的问题可以使用随机选择(列表(方法来解决
但我要指出的其他可以极大地缩短代码的事情是开始在类中学习。这比看起来容易得多,但类将允许您创建一个"玩家"类,该类具有名为score、name或card_drawn的对象,因此players.name='Mike'、players.score=27等。
我还重组了你的职能,并将其放在首位。这样你的实际游戏就可以分开了。我非常松散地认为函数是改变程序运行方式的标签。如果您曾经使用过命令提示符GOTO命令,那么它几乎可以以某种小的方式被视为相同的方式。只要函数已经定义,程序就会停止,并转到函数,无论它在哪里。
如果你有任何问题,请随时问我,你甚至可能也帮我解决一些问题。