Python 如何将数组转移到另一个子程序?
我在一个子程序中有一个数组,我想在一个新的子程序中重用该数据。我该怎么做 我听说过使用字典,但我不确定如何继续使用。 代码越简单越好,谢谢Python 如何将数组转移到另一个子程序?,python,dictionary,Python,Dictionary,我在一个子程序中有一个数组,我想在一个新的子程序中重用该数据。我该怎么做 我听说过使用字典,但我不确定如何继续使用。 代码越简单越好,谢谢 def optiona(): playernames = ['A', 'B', 'C', 'D', 'E', 'F'] numjudges = 5 playerscores = [] scoresfile = open('scores.txt', 'w') for players in playernames: string = []
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)
在这之后,我得到了一个错误:round2()缺少1个必需的位置参数:“playerscores”
您需要从
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中是至关重要的 什么是子程序?另外,请不要一次问两个完全无关的问题。@PM2Ring like def main()@PM2Ring此错误有什么特殊原因吗
TypeError:round2()缺少1个必需的位置参数:“playerscores”
@PM2Ring yes完全正确,我想在其他“函数”中访问。抱歉,我以为它被称为@PM2Ring的子gramedited