Python有没有一种方法可以将函数放入函数中而不获取“TypeError:'int'object Is not callable”?

2024-04-20 06:46:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个多人掷骰子游戏,你得到的点数取决于你掷什么。我试图把一个函数内的一个函数,但这不工作,由于typeerror

我在网上搜索了如何减少函数之间的交互,试图从另一个函数中删除一个函数,但是没有答案。我也问过我的同龄人和老师,但他们也不知道

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停止,它会显示indexeror

我需要这段代码继续工作的代码的其余部分,或为我修复我的代码中的任何错误


Tags: inforifdefrangepointsplayerprint
2条回答

玩家点数=[0,0]
玩家=2

通过执行playerPoints[player],您试图访问不存在的第3个元素。列表索引从0开始

你可以做playerPoints[player-1]

按c键时的第一个错误是由于相同的变量和函数名“points”引起的。第二个错误是由于列表的索引不足而导致的,可以通过将索引减去1来修复

这是一个固定的代码

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

为了将来的参考,最好的编程实践是给出适当的变量和函数名,以避免混淆和更糟糕的bug

相关问题 更多 >