1

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.

Mohamed Ismail
  • 27
  • 2
  • 12
  • 1
    Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Mike Scotty May 02 '18 at 09:02

2 Answers2

1

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.

hspandher
  • 15,934
  • 2
  • 32
  • 45
0

Just create a list before your while loop, to which you then append a new list with the data at the end of the while loop. This way you will end up with a list of lists.

Luca9984
  • 141
  • 6