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

时间:2017-10-05 15:57:57

标签: python dictionary

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

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

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'

1 个答案:

答案 0 :(得分:1)

您需要从playerscores返回optiona列表,以便将其传递到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中正确的缩进是至关重要的