如何在Python上打印多个列表

时间:2015-04-26 14:17:41

标签: python-3.x

我正在制作一个我尚未完成的基本食谱查看器,我遇到的一个问题是,当我将食谱附加到列表并打印每个列表时,此错误会出现在Python shell中;粗体代表用户输入

欢迎来到Mohamed Ismail的食谱查看器

添加配方(添加),加载配方(加载)或删除配方(删除)

添加

输入食谱ID = 1

输入食谱名称= 蛋糕

摘要

Recipe id = 1

蛋糕食谱

保存食谱?

...

成功保存

添加配方(添加),加载配方(加载)或删除配方(删除)

负载

以下是您现有的食谱ID

加载...

[' 1']

按ID 1

选择食谱

[(' Recipe id =',' 1')]

[<function function.<locals>.fileb at 0x105920400>] # this is the error

回去?或退出

这是我的代码,请帮助解决这个问题,非常感谢

import time

currentrecipes = []
rp1 = []
rp2 = []


print("Welcome to Mohamed Ismail's Recipe Viewer")
time.sleep(1)

def function():
     print('Add Recipe (add), Load Recipe (load) or Delete Recipe (delete)')
     choice = input()
     if choice == 'add':
            a = input('Input recipe id = ')
            b = input('Input recipe name = ')
            filea = ("Recipe id =",a)
            fileb = ("Recipe for =",b)
            print('')
            print("Summary")
            print("------------------")
            file1 = print("Recipe id =",a)
            file2 = print("Recipe for",b)

            def file():
                return filea
            def fileb():
                return fileb
            file()
            fileb()
            print('Save Recipe?')
            userinput = input()
            if userinput == 'Yes':
                 if a == '1':
                    currentrecipes.append(a)
                    rp1.append(file())
                    rp2.append(fileb())
                    print('...')
                    time.sleep(0.5)
                    print("Saved Sucesfully")
                    function()
                 else:
                      print("Choose a recipe id of 1")
                      print("Going back...")
                      time.sleep(2)
                      function()
            else:
                 function()
     elif choice == 'load':
          if currentrecipes == []:
             print("Error 619: You have no current recipes")
             function()
          else:
             print("Here are your existing recipe id's")
             print("Loading...")
             time.sleep(2)
             print(currentrecipes)
             option = input("Select Recipe by id ")
             if option == '1':
                 print(rp1)
                 print(rp2)
                 userchoice = input("Go back? or exit ")
                 if userchoice == 'Go back':
                     function()
                 else:
                      exit()    


     elif choice == 'delete':
          if currentrecipes == []:
             print("Error 619: You have no current recipes")
             function()
          else:

              print("Here are your existing recipe id's")
              print("Loading...")
              time.sleep(2)
              print(currentrecipes)
              option = input("Which recipe do you wish to delete ")
              if option == '1':
                   if '1' not in currentrecipes:
                       print("Error 404: Recipe is not in saved recipes")
                       function()
                   else:
                       currentrecipes.remove('1')
                       print('Loading...')
                       time.sleep(1)
                       print(currentrecipes)
                       time.sleep(0.5)
                       print("Deleted Succesfully")

     else:
          print("Please select one of the 3 options")
          time.sleep(1)
          function()

function()

1 个答案:

答案 0 :(得分:1)

假设您期望输出为[('Recipe for =','Cake')],当您使用与元组相同的名称定义函数fileb时,会出现问题。因此,fileb()现在返回一个函数而不是你想要返回的元组。