如何将数组传到另一个子程序?

2024-09-28 04:24:05 发布

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

我在一个子程序中有一个数组,我想在一个新的子程序中重用这个数据。我该怎么做?你知道吗

我听说过使用词典,但我不知道如何继续这样做。 代码越简单越好,谢谢。你知道吗

def optiona():

playernames = ['A', 'B', 'C', 'D', 'E', 'F']

numjudges = 5

playerscores = []
scoresfile = open('scores.txt', 'w')

for players in playernames:
    string = []
    for z in range(5):
        print("Enter score from Judge", z+1, "for Couple ", players, "in round 1:")
        data = input()
        playerscores.append(int(data))
        string.append(data)
    scoresfile.write(','.join(string) + '\n')
    print()
print('Registration complete for round 1')
scoresfile.close()
round2()

def round2(playerscores):

          print(playerscores)

在这之后,我得到了这个错误TypeError: round2() missing 1 required positional argument: 'playerscores'


Tags: infordatastringdef数组printplayers
1条回答
网友
1楼 · 发布于 2024-09-28 04:24:05

您需要从optiona返回playerscores列表,以便将其传递到round2。我已经将string的名称改回row,因为string是标准模块的名称。使用它作为变量名在这里不会有什么坏处,但是最好不要隐藏标准名。你知道吗

def optiona(playernames, numjudges):
    playerscores = []
    scoresfile = open('scores.txt', 'w')

    for players in playernames:
        row = []
        for z in range(1, numjudges + 1):
            print("Enter score from Judge", z, "for couple ", players, "in round 1:")
            data = input()
            playerscores.append(int(data))
            row.append(data)
        scoresfile.write(','.join(row) + '\n')
        print()
    scoresfile.close()

    return playerscores

def round2(playerscores):
    print(playerscores)

playernames = ['A', 'B', 'C', 'D', 'E', 'F']
numjudges = 5

playerscores = optiona(playernames, numjudges)
round2(playerscores)

顺便说一句,您在问题中发布的代码没有正确缩进。您需要小心,因为正确的缩进在Python中是非常重要的。你知道吗

相关问题 更多 >

    热门问题