0

I have an array in a sub program and I want to re-use that data in a new subprogram. how would I do this?

I have heard about using a dictionary but i am not sure about how to proceed on doing so. The simpler the code the better, thank you.

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)

now after this, i get this error TypeError: round2() missing 1 required positional argument: 'playerscores'

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
somebrick
  • 3
  • 6
  • What's a sub program? Also, please don't ask two totally unrelated questions at a time. – PM 2Ring Oct 05 '17 at 16:03
  • @PM2Ring like def main() – somebrick Oct 05 '17 at 16:05
  • @PM2Ring is there any particular reason for this error? `TypeError: round2() missing 1 required positional argument: 'playerscores'` – somebrick Oct 05 '17 at 16:09
  • @PM2Ring yes exactly, i want to access in a different 'function'. sorry thought it was referred to as a subprogram – somebrick Oct 05 '17 at 16:11
  • edited @PM2Ring – somebrick Oct 05 '17 at 16:27
  • When you call `round2()` you never pass the first argument (`playerscores`) – Edward Minnix Oct 05 '17 at 16:31
  • To future readers: this question is a follow-on from [this question](https://stackoverflow.com/questions/46570575/how-to-write-to-a-file-in-an-organized-manner). – PM 2Ring Oct 05 '17 at 16:46
  • @PM2Ring please read edit in main Q – somebrick Oct 05 '17 at 17:25
  • Please don't make major changes to the question after it has (relevant) answers, since that can invalidate those answers. If you have a new question, then ask a new question. You can link the new question to the old one if the old question has information that's useful to answering the new one. – PM 2Ring Oct 05 '17 at 17:32
  • those major changes had nothing to do with the problem though... hence why i felt it was unnecessary – somebrick Oct 05 '17 at 17:39
  • Those changes removed most of the code! And it made my answer look like it had nothing to do with your question. Questions on Stack Exchange sites are supposed to be helpful to future readers as well as to the person who originally asked the question. Please try to keep that in mind. – PM 2Ring Oct 05 '17 at 18:22
  • If you have a new question on this topic please ask a new question, as I suggested earlier. – PM 2Ring Oct 05 '17 at 18:23

1 Answers1

1

You need to return the playerscores list from optiona so you can pass it into round2. I've changed the name of string back to row because string is the name of a standard module. Using it as a variable name won't hurt anything here, but it's best to not shadow the standard names.

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)

BTW, the code you posted in your question is not indented correctly. You need to be careful about that, since correct indentation is vital in Python.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182