Python - 有没有办法将函数放入函数而不是“TypeError:'int' object is not callable”?

匿名的

我正在制作一个多人掷骰子游戏,你可以根据你掷出的东西获得积分。我试图将一个函数放入一个函数中,但是由于类型错误这不起作用。

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

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 Python 3.x 中出错:TypeError: 'int' object is not callable

'TypeError: 'int' object is not callable' 比较相隔 5 秒的读数时

我如何解决这个 TypeError: 'int' object is not callable?

对于 i in range(len(data)): TypeError: 'int' object is not callable

有没有办法两次从boto3 python lambda函数调用“ s3.Object()”

有没有办法在使用循环时将函数的输出放入python中的数组中?

使用 __getattr__ 导致 TypeError: 'str' object is not callable - Python 2.7

如何修复 Python 中的“TypeError: 'str' object is not callable”错误?

Python:如何解决“TypeError:'str' object is not callable”

有没有办法镜像python函数?

有没有办法反转这个python函数?

为什么我会收到 TypeError: 'int' object not callable in a micro:bit MicroPython 程序?

有没有办法在python 3中设置int的限制

有没有办法从FutureTask取回Callable?

有没有办法用二维数组(int)重载构造函数?

有没有办法在 c# 中的 int[][] 上使用函数相交

有没有办法从 C++ 函数中的 int 变量返回浮点数?

有没有办法将OpenCV的imshow()函数集成到python中的kivy或kv文件中

有没有办法将原始输入存储在应用于python中数据框的函数中?

有没有办法将完整的代码块定义为变量作为 python 上 goto 函数的替代?

有没有办法使此Python kNN函数更有效?

Python tkinter Entry 小部件验证错误:TypeError:'str' object is not callable

为什么我在 python(discord bot) 中出现以下错误:TypeError: 'str' object is not callable

如何在 Python 中解决此错误:“TypeError: ‘str’ object is not callable”

尝试通过索引获取元组中的对象时,Python 中出现“TypeError: 'tuple' object is not callable”错误

range() 函数产生错误 'TypeError: 'str' object is not callable'

我不断收到 TypeError: 'str' object is not callable

有没有办法将密钥而不是文件传递给python sshtunnel?

有没有办法在 Python 的函数中使用可选参数?