我正在创建一个简单的python程序,该程序将用户输入的值存储在数组中。该程序最多询问用户8次(可以输入8条数据)。我需要程序在每次通过while循环时创建一个新数组。因此,如果i = 1,则每次循环通过时,i = i + 1,直到i

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


最后几行肯定是错误的,但是希望这可以让我了解我正在尝试做什么。

最佳答案

只需保留一个列表,然后将列表追加到该列表即可。

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)


甚至更好,使用一个功能-

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:尝试使用Pythonic命名约定。

10-06 12:34