Ad

How To Create A New Array Every Time A While Loop Passes

I am creating a simple python program that stores values entered by the user in an array. The program will ask the user up to 8 times ( 8 pieces of data can be entered). I need the program to create a new array every time it passes over the while loop. So if i = 1, And every time the loop passes it does i = i + 1, until i < 9. The loop should create a total of 8 arrays. Below is what ive been working on, the code is very simple, your insights will be helpful. The context of the code is a simple Athlete gender form.

i = 1
while i < 9:
    Question = input("Inputting data for Lane", i)
    Gender = input("Is the athlete male or female ")
    Athlete = input("What is the athletes name ")
    Dataset = ("Gender =", Gender , "Athlete = ", Athlete)
    Racer + i = []
    (Racer + i).append(Dataset)
    i = i + 1

The last few lines definitely are wrong however hopefully this can give an insight in what im trying to do.

Ad

Answer

Just keep a list and keep appending lists to it.

datasets = []     

for i in range(0, 9):
    Question = input("Inputting data for Lane", i)
    Gender = input("Is the athlete male or female ")
    Athlete = input("What is the athletes name ")
    Dataset = ("Gender =", Gender , "Athlete = ", Athlete)

    datasets.append(Dataset)

or even better, use a function -

def get_dataset():
    Question = input("Inputting data for Lane", i)
    Gender = input("Is the athlete male or female ")
    Athlete = input("What is the athletes name ")

    return ("Gender =", Gender , "Athlete = ", Athlete)

datasets = [get_dataset() for _ in range(0, 9)]

PS: Try to use Pythonic naming conventions.

Ad
source: stackoverflow.com
Ad