Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python打印具有无限用户输入的2D列表_Python - Fatal编程技术网

Python打印具有无限用户输入的2D列表

Python打印具有无限用户输入的2D列表,python,Python,我试图完成这段代码,它会要求用户输入他们的名字和他们最喜欢的食物是什么,它会不断地询问用户是否想要添加更多的数据。在用户输入'N'时,我需要将整个表格连同所有输入一起打印出来,有人知道如何做到这一点,而不是一个接一个 def get_string_inputs (p) : ''' requests the string data and returns it''' strData=input("Please enter {0} : ".format(p)) return

我试图完成这段代码,它会要求用户输入他们的名字和他们最喜欢的食物是什么,它会不断地询问用户是否想要添加更多的数据。在用户输入'N'时,我需要将整个表格连同所有输入一起打印出来,有人知道如何做到这一点,而不是一个接一个

def get_string_inputs (p) :
    ''' requests the string data and returns it'''
    strData=input("Please enter {0} : ".format(p))
    return strData

def add_data_rows () :
    ''' adds data to the row array'''
    meal_details = [ ]
    #the helper function is used here
    name = get_string_inputs("your name ").capitalize()
    meal_details.append(name)
    favourite_meal = get_string_inputs("your favourite food").capitalize()
    meal_details.append(favourite_meal)
    return meal_details

def main() :
    '''runs all functions'''
    favMeal = []
    header=['Name','Favourite Meal']
    favMeal.append(header)

    meal_details = add_data_rows()
    favMeal.append(meal_details)
    for a,b in favMeal:
        print('{0:16}{1:<16}'.format(a,b))
    while True:    
        valid_option = ['Y','N']
        question = input("Would you like to add more data? (Y/N): ").upper()
        if question in valid_option:
            if question == 'Y' :
                main()
            if question == 'N' :
                ??????
        break
    else:
        print("That is not a valid choice, Please enter Y or N")
main()
def get_string_输入(p):
''请求字符串数据并返回它''
strData=input(“请输入{0}:”.format(p))
返回标准数据
def添加数据行():
''将数据添加到行数组''
用餐详情=[]
#这里使用helper函数
name=获取字符串输入(“您的姓名”).capitalize()
用餐详情。附加(姓名)
最喜欢的食物=获取字符串输入(“你最喜欢的食物”)。大写()
膳食详细信息。附加(最喜爱的膳食)
回餐详情
def main():
''运行所有函数''
favMeal=[]
header=['Name','favorite meat']
favMeal.append(头)
用餐详情=添加数据行()
favMeal.append(膳食详情)
对于favMeal中的a、b:

打印({0:16}{1:你有几件事不对劲,我在我更改的行上做了笔记。 我希望这有帮助

def get_string_inputs (p) :
    ''' requests the string data and returns it'''
    strData=input("Please enter {0} : ".format(p))
    return strData

def add_data_rows () :
    ''' adds data to the row array'''
    meal_details = [ ]
    #the helper function is used here
    name = get_string_inputs("your name ").capitalize()
    meal_details.append(name)
    favourite_meal = get_string_inputs("your favourite food").capitalize()
    meal_details.append(favourite_meal)
    return meal_details

def main() :
    '''runs all functions'''
    favMeal = []
    header=['Name','Favourite Meal']  # Add the header row
    favMeal.append(header)
    meal_details = add_data_rows()  # Now add the first row
    favMeal.append(meal_details)
    valid_option = ['Y', 'N']  # Just do this once, not every time the loop happens
    while True:  # Now add rows until they say N
        question = input("Would you like to add more data? (Y/N): ").upper()
        if question in valid_option:
            if question == 'Y' :
                favMeal.append(add_data_rows())  # Here we just call the function that adds a row, and append the results that it returns
            if question == 'N' :
                break # Break the while loop if they answer N
        else:
            print("That is not a valid choice, Please enter Y or N")
    for a,b in favMeal: # Now print everything
        print('{0:16}{1:<16}'.format(a,b))
main()
def get_string_输入(p):
''请求字符串数据并返回它''
strData=input(“请输入{0}:”.format(p))
返回标准数据
def添加数据行():
''将数据添加到行数组''
用餐详情=[]
#这里使用helper函数
name=获取字符串输入(“您的姓名”).capitalize()
用餐详情。附加(姓名)
最喜欢的食物=获取字符串输入(“你最喜欢的食物”)。大写()
膳食详细信息。附加(最喜爱的膳食)
回餐详情
def main():
''运行所有函数''
favMeal=[]
header=['Name','favorite meat']#添加标题行
favMeal.append(头)
用餐详情=添加数据行()#现在添加第一行
favMeal.append(膳食详情)
valid_option=['Y','N']#只需执行一次,而不是每次循环发生时
如果为True:#现在添加行,直到它们显示N
问题=输入(“是否要添加更多数据?(Y/N):”)。上限()
如果问题在有效选项中:
如果问题==“Y”:
append(add_data_rows())#这里我们只调用添加一行的函数,并附加它返回的结果
如果问题=='N':
中断#如果他们回答N,则中断while循环
其他:
打印(“该选项无效,请输入Y或N”)
对于favMeal中的a、b:#现在打印所有内容

打印({0:16}{1:你有几件事不对劲,我在我更改的行上做了笔记。 我希望这有帮助

def get_string_inputs (p) :
    ''' requests the string data and returns it'''
    strData=input("Please enter {0} : ".format(p))
    return strData

def add_data_rows () :
    ''' adds data to the row array'''
    meal_details = [ ]
    #the helper function is used here
    name = get_string_inputs("your name ").capitalize()
    meal_details.append(name)
    favourite_meal = get_string_inputs("your favourite food").capitalize()
    meal_details.append(favourite_meal)
    return meal_details

def main() :
    '''runs all functions'''
    favMeal = []
    header=['Name','Favourite Meal']  # Add the header row
    favMeal.append(header)
    meal_details = add_data_rows()  # Now add the first row
    favMeal.append(meal_details)
    valid_option = ['Y', 'N']  # Just do this once, not every time the loop happens
    while True:  # Now add rows until they say N
        question = input("Would you like to add more data? (Y/N): ").upper()
        if question in valid_option:
            if question == 'Y' :
                favMeal.append(add_data_rows())  # Here we just call the function that adds a row, and append the results that it returns
            if question == 'N' :
                break # Break the while loop if they answer N
        else:
            print("That is not a valid choice, Please enter Y or N")
    for a,b in favMeal: # Now print everything
        print('{0:16}{1:<16}'.format(a,b))
main()
def get_string_输入(p):
''请求字符串数据并返回它''
strData=input(“请输入{0}:”.format(p))
返回标准数据
def添加数据行():
''将数据添加到行数组''
用餐详情=[]
#这里使用helper函数
name=获取字符串输入(“您的姓名”).capitalize()
用餐详情。附加(姓名)
最喜欢的食物=获取字符串输入(“你最喜欢的食物”)。大写()
膳食详细信息。附加(最喜爱的膳食)
回餐详情
def main():
''运行所有函数''
favMeal=[]
header=['Name','favorite meat']#添加标题行
favMeal.append(头)
用餐详情=添加数据行()#现在添加第一行
favMeal.append(膳食详情)
valid_option=['Y','N']#只需执行一次,而不是每次循环发生时
如果为True:#现在添加行,直到它们显示N
问题=输入(“是否要添加更多数据?(Y/N):”)。上限()
如果问题在有效选项中:
如果问题==“Y”:
append(add_data_rows())#这里我们只调用添加一行的函数,并附加它返回的结果
如果问题=='N':
中断#如果他们回答N,则中断while循环
其他:
打印(“该选项无效,请输入Y或N”)
对于favMeal中的a、b:#现在打印所有内容

打印({0:16}{1:那么你的意思是你只想通过一次调用
print
来输出列表,而不是在列表中循环打印数据?那么你的意思是你只想通过一次调用
print
来输出列表,而不是在列表中循环打印数据?效果很好,没有意识到你不需要把m在循环中,所以它会重复。谢谢!工作完美,没有意识到你不必把主管道放在循环中,所以它会重复。谢谢!