我正在制作一个多人掷骰子游戏,你可以根据你掷出的东西获得积分。我试图将一个函数放入一个函数中,但是由于类型错误这不起作用。
我在网上搜索了如何减少函数之间的交互以尝试从另一个函数中删除一个函数,但是没有答案。也问过我的同龄人和老师,但他们也不知道。
import random
playerPoints = []
minPlayers = 2
players = 0
maxscore = 100
amountOfDice = 2
gameRound = 0
toRoll = ""
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
def setPlayers():
while True:
players = input("How many players are playing?\n")
if players.isdigit():
players = int(players)
if minPlayers <= players:
for i in range(players):
playerPoints.append(0)
return players
def diceroll(player, amountOfDice, die1, die2):
throw = 0
print("Player {0}s turn:".format(player + 1))
for i in range(amountOfDice):
roll = 0
while roll != toRoll:
roll = input("Press enter to roll both dice")
if roll == toRoll:
print("Player {0} has thrown {1} and {2}".format(player + 1, i + 1, die1, die2))
break
points()
def cont():
cont = input("Would you like to continue or stop? Press [C] to continue or press [S] to stop.")
if cont == "c":
points()
elif cont == "s":
print("You have successfully cashed in " + str(points) + " points. Well done!")
playerPoints[players] += points
playerPoints[player] += throw
print("Player {0}s score is now: {1}".format(player + 1, playerPoints[player]))
return throw
def checkWin(maxscore):
for player in range(players):
if (playerPoints[player] >= maxscore):
print("Player {0} wins!".format(player + 1))
return True
return False
def points():
while die1 != 1 or die2 != 1 and cont == "c":
for i in playerPoints:
global points
points = 0
if die1 == 1 and die2 == 1:
print("That means you store " + str(double1) + " points.")
points += double1
cont()
elif die1 == die2:
print("That means you store " + str((die1 + die2)*2) + " points.")
points += (die1 + die2)*2
cont()
elif die1 != die2:
print("That means you store " + str(die1 + die2) + " points.")
points += die1 + die2
cont()
elif die1 == 1 or die2 == 2:
print("Unlucky! You have rolled " + str(die1) + " and " + str(die2) + ". Since you rolled a 1, your turn has ended. You have lost all stored points so far, and you lose " + str(die1 + die2) + " points.")
points -= (die1 + die2)
if __name__ == "__main__":
players = setPlayers()
while True:
gameRound += 1
print("Round: {0}".format(gameRound))
for i in range(players):
diceroll(i, amountOfDice, die1, die2)
if (checkWin(maxscore)):
break
def players(numberOfPlayers):
numberOfPlayers = 0
while numberOfPlayers not in (str(i) for i in range (minPlayers,maxPlayers)):
numberOfPlayers = int(numberOfPlayers)
for i in range(numberOfPlayers):
playerPoints["score{}".format(i+1)] = 0
return numberOfPlayers
请将代码粘贴到 python IDLE 或其他东西中以尝试代码。
所以基本上它从询问有多少玩家开始。我输入 2,因为这是最小值。
然后它要求按回车键滚动模具,这工作得很好。
它最后询问我是要继续还是停止,当我按 c 继续时,它说 TypeError。
如果我按 s 停止,它会显示 IndexError。
我需要此代码才能继续运行其余代码,或者让我修复代码中的任何错误。
按 c 时的第一个错误是由于相同的变量和函数名称“点”引起的。第二个错误是由于列表用完索引引起的,可以通过将索引减一来修复。
这是一个固定的代码,
import random
playerPoints = []
minPlayers = 2
players = 0
maxscore = 100
amountOfDice = 2
gameRound = 0
toRoll = ""
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
def setPlayers():
while True:
players = input("How many players are playing?\n")
if players.isdigit():
players = int(players)
if minPlayers <= players:
for i in range(players):
playerPoints.append(0)
return players
def diceroll(player, amountOfDice, die1, die2):
throw = 0
print("Player {0}s turn:".format(player + 1))
for i in range(amountOfDice):
roll = 0
while roll != toRoll:
roll = input("Press enter to roll both dice")
if roll == toRoll:
print("Player {0} has thrown {1} and {2}".format(player + 1, i + 1, die1, die2))
break
func_points()
def cont():
cont = input("Would you like to continue or stop? Press [C] to continue or press [S] to stop.")
if cont == "c":
func_points()
elif cont == "s":
print("You have successfully cashed in " + str(points) + " points. Well done!")
print (playerPoints, players)
playerPoints[players - 1] += points
playerPoints[players - 1] += throw
print("Player {0}s score is now: {1}".format(player + 1, playerPoints[player]))
return throw
def checkWin(maxscore):
for player in range(players):
if (playerPoints[player] >= maxscore):
print("Player {0} wins!".format(player + 1))
return True
return False
def func_points():
while die1 != 1 or die2 != 1 and cont == "c":
for i in playerPoints:
global points
points = 0
if die1 == 1 and die2 == 1:
print("That means you store " + str(double1) + " points.")
points += double1
cont()
elif die1 == die2:
print("That means you store " + str((die1 + die2)*2) + " points.")
points += (die1 + die2)*2
cont()
elif die1 != die2:
print("That means you store " + str(die1 + die2) + " points.")
points += die1 + die2
cont()
elif die1 == 1 or die2 == 2:
print("Unlucky! You have rolled " + str(die1) + " and " + str(die2) + ". Since you rolled a 1, your turn has ended. You have lost all stored points so far, and you lose " + str(die1 + die2) + " points.")
points -= (die1 + die2)
if __name__ == "__main__":
players = setPlayers()
while True:
gameRound += 1
print("Round: {0}".format(gameRound))
for i in range(players):
diceroll(i, amountOfDice, die1, die2)
if (checkWin(maxscore)):
break
def players(numberOfPlayers):
numberOfPlayers = 0
while numberOfPlayers not in (str(i) for i in range (minPlayers,maxPlayers)):
numberOfPlayers = int(numberOfPlayers)
for i in range(numberOfPlayers):
playerPoints["score{}".format(i+1)] = 0
return numberOfPlayers
为了将来参考,给出适当的变量和函数名称是一个很好的编程习惯,以避免混淆和更糟糕的错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句